Jak ustawić programowo rysowane tło w Androidzie
Aby ustawić tło:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
Czy to najlepszy sposób? 11 answers
layout.setBackgroundResource(R.drawable.ready);
zgadza się.
Innym sposobem, aby to osiągnąć, jest użycie następującego:
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready) );
} else {
layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
}
Ale myślę, że problem występuje, ponieważ próbujesz załadować duże obrazy.
tutaj {[15] } jest dobry samouczek Jak ładować Duże bitmapy.
UPDATE:
getDrawable (int ) deprecated in API level 22 getDrawable(int )
jest teraz przestarzały w API na poziomie 22.
Zamiast tego powinieneś użyć następującego kodu z biblioteki wsparcia:
ContextCompat.getDrawable(context, R.drawable.ready)
Jeśli odnosisz się do kod źródłowy ContextCompat.getDrawable , daje coś takiego:
/**
* Return a drawable object associated with a particular resource ID.
* <p>
* Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
* drawable will be styled for the specified Context's theme.
*
* @param id The desired resource identifier, as generated by the aapt tool.
* This integer encodes the package, type, and resource entry.
* The value 0 is an invalid identifier.
* @return Drawable An object that can be used to draw this resource.
*/
public static final Drawable getDrawable(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 21) {
return ContextCompatApi21.getDrawable(context, id);
} else {
return context.getResources().getDrawable(id);
}
}
Więcej szczegółów na ContextCompat
Począwszy od API 22, powinieneś używać metody getDrawable(int, Theme)
zamiast getDrawable (int).
UPDATE:
Jeśli korzystasz z biblioteki support v4, następujące informacje będą wystarczające dla wszystkich wersji.
ContextCompat.getDrawable(context, R.drawable.ready)
Musisz dodać następujące elementy do swojej aplikacji.gradle
compile 'com.android.support:support-v4:23.0.0' # or any version above
Lub za pomocą ResourceCompat, w dowolnym API jak poniżej:
import android.support.v4.content.res.ResourcesCompat;
ResourcesCompat.getDrawable(getResources(), R.drawable.name_of_drawable, null);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-01 07:33:54
Spróbuj tego:
layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
I dla API 16<:>
layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-30 19:32:20
RelativeLayout relativeLayout; //declare this globally
Teraz, wewnątrz dowolnej funkcji jak onCreate, onResume
relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.view); //or whatever your image is
setContentView(relativeLayout); //you might be forgetting this
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-08 09:36:11
Możesz również ustawić tło dowolnego obrazu:
View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-28 13:11:33
Jeśli Twoje tła znajdują się teraz w folderze drawable, spróbuj przenieść obrazy z drawable do drawable-nodpi w swoim projekcie. To działało dla mnie, wydaje się, że w przeciwnym razie obrazy są przeskalowane przez nich samych..
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-17 12:44:29
Używam minSdkVersion 16 i targetSdkVersion 23 Poniżej działa dla mnie, używa ContextCompat.getDrawable (context, R. drawable.drawable);
Zamiast używać:layout.setBackgroundResource(R.drawable.ready);
Raczej użyj:
layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
getActivity()
jest używany w fragmencie, jeśli wywołanie z działania use this
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-30 10:36:40
Użyj butterknife , Aby powiązać zasób drawable ze zmienną, dodając go do góry swojej klasy (przed jakimikolwiek metodami).
@Bind(R.id.some_layout)
RelativeLayout layout;
@BindDrawable(R.drawable.some_drawable)
Drawable background;
Następnie wewnątrz jednej z metod dodaj
layout.setBackground(background);
That ' s all you need
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-22 13:54:53
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.ready));
else if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
layout.setBackground(getResources().getDrawable(R.drawable.ready));
else
layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-26 00:29:27
Wypróbuj ten kod:
Drawable thumb = ContextCompat.getDrawable(getActivity(), R.mipmap.cir_32);
mSeekBar.setThumb(thumb);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-28 07:56:53
Spróbuj ViewCompat.setBackground(yourView , drawableBackground)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-19 10:32:18
Wewnątrz app / res / your_xml_layout_file .xml
- przypisz nazwę do układu nadrzędnego.
- przejdź do głównej aktywności i znajdź RelativeLayout, wywołując findViewById(R.id. "given_name").
- użyj układu jako klasycznego obiektu, wywołując metodę setBackgroundColor ().
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-08 14:49:08