Пользовательские атрибуты XML без пользовательского просмотра во фрагменте
Я пытаюсь добавить пользовательские атрибуты XML в существующий вид
Добавление их в пользовательское представление не представляет особой проблемы, но я не знаю, как получить доступ к этим атрибутам в «базовом» представлении (например, TextView, LinearLayout, ImageView ...)
И это сложнее, когда задействован проект Fragment и / или Library
Пока вот мой код
Определение таможенных атрибутов и XML (attrs.xml и макет):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="StarWars">
<attr name="jedi" format="string" />
<attr name="rank" format="string" />
</declare-styleable>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sw="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dp"
android:gravity="center_horizontal"
sw:jedi="Obiwan" />
<TextView
android:id="@+id/rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dp"
android:gravity="center_horizontal"
sw:rank="Master" />
Так как я раздуваю это на фрагменте (так что onCreate с attrs arg!), Единственный способ получить пользовательские атрибуты 2 sw - это:
Установите пользовательский LayoutInflater.Factory для фрагмента LayoutInflater в onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
LayoutInflater layoutInflater = inflater.cloneInContext(inflater.getContext());
layoutInflater.setFactory(new StarWarsLayoutFactory());
View fragmentView = layoutInflater.inflate(R.layout.jedi, container, false);
((TextView) fragmentView.findViewById(android.R.id.jedi)).setText("Yep");
return fragmentView;
}
Попытка получить пользовательские атрибуты в пользовательском LayoutInflater.Factory:
public class StarWarsLayoutFactory implements Factory {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
*** What to do here ? ***
return null;
}
}
У кого-нибудь был такой вопрос?
Что мне здесь не хватает?
Спасибо заранее!