Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

java - Android Set Custom Preference Layout

I'm working on it Android project. I have a prefs.xml code, something like this

<?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>

And I need to custom preference layout. And I created;

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>

And write a 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);
    }
}

My problem is; I write setBackgroundColor method but not working. Not working meanin is, this program is running without error (like NullReferenceException, there is no error). But background color still not changing.

I don't know why. How can i solve this problem? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Obviously, if you are hard-coding the color then you can just do it in your XML:

android:background="@android:color/red"

If you want to do it in code then unfortunately it's trickier than it might seem. You can't just set the color of the preference view in onCreate() because the preference views are stored in a list and are created and recycled dynamically as you scroll the list.

You need to set the background color when the view is being created. To do that you'll need to implement a custom preference class and override getView():

public class CustomColorPreference extends Preference
{
    int backgroundColor = Color.BLACK;

    public CustomColorPreference(Context context) {
        super(context);
    }

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

    public void setCustomBackgroundColor(int color)
    {
        backgroundColor = color;
    }

    @Override
    public View getView(View convertView, ViewGroup parent)
    {
        View v = super.getView(convertView, parent);

        // v.setBackgroundColor(backgroundColor); // set background color of whole view
        ImageView ivNameTextColor = (ImageView)v.findViewById(R.id.ivNameTextColor);
        ivNameTextColor.setBackgroundColor(backgroundColor);

        return v;
    }
}

Change your XML to use the CustomColorPreference class:

<com.example.yourapp.CustomColorPreference
        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" />

Then in your onCreate you can get the CustomColorPreference and set the color on it using the public method setCustomBackgroundColor():

CustomColorPreference picker = (CustomColorPreference)findPreference("pref_name_color_picker");
picker.setCustomBackgroundColor(Color.RED);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...