Я работаю над проектом Android. У меня есть код prefs.xml, что-то вроде этогоAndroid Set Custom Preference Layout
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="pref_name_color_picker"
android:title="Colour"
android:summary="Colour of the name"
android:defaultValue="#FFFFFF"
android:layout="@layout/custom_name_setting_layout" />
</PreferenceCategory>
</PreferenceScreen>
И мне нужна индивидуальная схема предпочтений. И я создал;
custom_name_setting_layout.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:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView
android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView
android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignLeft="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2" />
<ImageView
android:id="@+id/ivNameTextColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="32dp"
android:minWidth="32dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
И написать SettingActivity.java
public class SettingActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
int color = 0xffffff00;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_name_setting_layout, null);
ImageView ivNameTextColor = (ImageView) row.findViewById(R.id.ivNameTextColor);
ivNameTextColor.setBackgroundColor(Color.RED);
}
}
Моя проблема; Я пишу метод setBackgroundColor, но не работает. Не работает meanin, эта программа работает без ошибок (например, NullReferenceException, ошибки нет). Но цвет фона по-прежнему не меняется.
Я не знаю почему. Как я могу решить эту проблему? Благодаря
Да, но идентификатор цвета будет меняться динамически. Этот образец, для образца, я пишу Color.RED –
Вам нужно работать с ListView. Вы можете вспомнить его с помощью 'findViewById (android.R.id.list)' – natario
Пожалуйста, дайте мне пример использования. Спасибо, что я новичок в кодировании Android. –