Тема / Стиль не применяется, когда инфлятор используется с ApplicationContext - PullRequest
43 голосов
/ 22 января 2010

У меня есть тема, которая определяет textColor для TextView как красный.

Я использую LayoutInflater для создания экземпляра TextView. Проблема в том, что стили не применяются к TextView, когда инфлятор создается с помощью ApplicationContext - цвет не красный. Все отлично работает, когда LayoutInflater создан с использованием активности.

Почему это происходит и как это можно исправить?

/ RES / значения / styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme">
        <item name="android:textViewStyle">@style/MyTextView</item>
    </style>

    <style name="MyTextView" parent="@android:style/Widget.TextView">
        <item name="android:textColor">#f00</item>
    </style>
</resources>

AndroidManifest.xml:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    >

Код:

public class A extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_a);

        final LayoutInflater goodInflater = getInflater((Activity)this); 
        final LayoutInflater badInflater = getInflater(getApplicationContext());
        final LinearLayout container = (LinearLayout)findViewById(R.id.container);

        findViewById(R.id.add_with_appContext).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add(container, badInflater); // Creates gray TextView
            }            
        });

        findViewById(R.id.add_with_activity).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add(container, goodInflater); // Creates red TextView
            }            
        });
    }

    private LayoutInflater getInflater(Context context) {
        return (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private void add(LinearLayout container, LayoutInflater inflater) {
        inflater.inflate(R.layout.my_template, container, true);
    }
}

/ Рез / макет / test_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button 
        android:text="Add with AppContext" 
        android:id="@+id/add_with_appContext" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

    <Button 
        android:text="Add with Activity" 
        android:id="@+id/add_with_activity" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"  
        />

</LinearLayout>

/ Рез / макет / my_template.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/text"
        android:text="Some text..."
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
    />

</LinearLayout>

Ответы [ 3 ]

50 голосов
/ 22 января 2010

Решение № 1

Метод inflate принимает необязательный аргумент 'ViewGroup root':

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

Если у нас есть значение для передачи в качестве корневого параметра, то, следовательно, мы можем использовать его для получения «контекста активности», откуда мы можем получить правильный LayoutInflater:

ViewGroup root > activity context > LayoutInflater

Так что мой код может быть:

private void add(LinearLayout container) {
    LayoutInflater inflater = getInflater(container.getContext());
    inflater.inflate(R.layout.my_template, container, true);
}

Решение № 2

Только что попытался установить тему Application Context программно, и она работает :

getApplicationContext().setTheme(R.style.MyTheme);

Я думаю, было бы логично ожидать эту разметку:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    >

, чтобы установить его автоматически, но это не так.

34 голосов
/ 04 августа 2014

Никогда не используйте контекст приложения для раздувания представлений, потому что стилизация не работает с этим контекстом. Всегда используйте контекст действия при игре с представлениями. Единственное исключение - когда вам нужно создать RemoteViews из службы.

Более подробную информацию о различных типах контекстов и их возможностях можно найти в этой превосходной статье .

0 голосов
/ 13 февраля 2017

Я обычно сталкиваюсь с этой проблемой при накачке пользовательского представления. Вот что я лично делаю, чтобы сохранить ту же тему действия в CustomView

public class CustomView extends ViewGroup{

public CustomView (Context context) {
    super(context);
    init(context);
}

public CustomView (Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public CustomView (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

@TargetApi(21)
public CustomView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context);
}

private void init(Context context) {
    LayoutInflater  mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = mInflater.inflate(R.layout.review_list_item, this, true);
    //rest of view initialization       
}   
}
...