Я пытаюсь объединить TextView и EditText в один составной элемент управления, который использует пользовательские элементы xml для передачи значений по умолчанию для каждого отдельного элемента. Я смотрел учебники / документы здесь:
Управление зданием
Передача пользовательских атрибутов
Что я до сих пор.
Attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="FreeText"> <attr name="label" format="string" /> <attr name="default" format="string" /> </declare-styleable> </resources>
Моя основная компоновка:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.misc.FreeText android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:label="label" myapp:default="default" /> </LinearLayout>
по<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.misc.FreeText android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:label="label" myapp:default="default" /> </LinearLayout>
по<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.misc.FreeText android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:label="label" myapp:default="default" /> </LinearLayout>
Мой составной контроль, FreeText:
public class FreeText extends LinearLayout { TextView label; EditText value; public FreeText(Context context, AttributeSet attrs) { super(context, attrs); this.setOrientation(HORIZONTAL); LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT); lp.weight = 1; label = new TextView(context); addView(label, lp); value = new EditText(context); addView(value, lp); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FreeText); CharSequence s = a.getString(R.styleable.FreeText_label); if (s != null) { label.setText(s); } a.recycle(); } }
поpublic class FreeText extends LinearLayout { TextView label; EditText value; public FreeText(Context context, AttributeSet attrs) { super(context, attrs); this.setOrientation(HORIZONTAL); LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT); lp.weight = 1; label = new TextView(context); addView(label, lp); value = new EditText(context); addView(value, lp); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FreeText); CharSequence s = a.getString(R.styleable.FreeText_label); if (s != null) { label.setText(s); } a.recycle(); } }
Когда я запускаю программу, я вижу представления ОК, но значение моего CharSequence s всегда равно null. Может ли кто-нибудь сказать мне, где я ошибаюсь?
Я ненавижу это, когда вы замечаете проблему сразу после того, как попросите о помощи.
Проблема заключалась в том, что мое пространство имен для моих пользовательских элементов XML должно быть таким:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.misc.FreeText android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:label="label" myapp:default="default" /> </LinearLayout>
по<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.misc.FreeText android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:label="label" myapp:default="default" /> </LinearLayout>
по<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.misc.FreeText android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:label="label" myapp:default="default" /> </LinearLayout>