我可以为所有Preference
项目设置背景样式。如何将一些Pereference
项设置为一种样式(例如设置listSelector
)并使用不同的样式设置其余Preference
项的样式?
我将Preference
的样式设置为设置其布局的样式:
<style name="Preference.Custom">
<item name="android:layout">@layout/preference_custom</item>
<item name="android:background">@color/transparent</item>
</style>
在preference_custom.xml
中,我再次设置背景:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/spacing_large"
android:gravity="center_vertical"
android:background="@color/transparent"> <--- Here's where I set the background
...
除了按下状态的背景外,所有自定义样式都会被设置。
我也尝试将背景设置为一个将按下状态设置为透明的drawable。
在我的自定义偏好类中,我设置了背景,我看到它从顶部条目更改为下面的条目:
android.graphics.drawable.ColorDrawable@41e30248
android.graphics.drawable.ColorDrawable@41de71d8
这是我的Preference
的子类:
public class PreferenceNoBackground extends Preference {
private Context mContext;
public PreferenceNoBackground(Context context) {
super(context);
mContext = context;
}
public PreferenceNoBackground(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public PreferenceNoBackground(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
view.setBackgroundResource(R.drawable.item_background_transparent);
}
}
item_background_transparent.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="@color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="@color/transparent" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="true" android:drawable="@color/transparent" />
<item android:drawable="@color/transparent" />
</selector>
我还尝试设置PreferenceFragment
:
pref = findPreference(Settings.CUSTOM);
pref.setOnPreferenceClickListener(this);
pref.getView(null, null).setBackground(getResources().getDrawable(
R.drawable.item_background_transparent));
答案 0 :(得分:0)
我之前通过为2个不同的列表项使用2种不同的布局来完成此操作。在我的情况下,我展示了一堆图像,然后是最终项目,这是一个网站链接。我的适配器中的相关代码如下所示:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (position >= imgIds.size())
{
// Can we use the convertView? Only if it was expanded from R.layout.help_list_item_last...
if (convertView != null && isLastView(convertView))
{
view = convertView;
}
if (view == null)
{
view = inflater.inflate(R.layout.help_list_item_last, parent, false);
if (view == null)
return null;
}
}
else
{
ImageView helpImage = null;
// Can we use the convertView? Only if it was expanded from R.layout.help_list_item...
if (convertView != null && !isLastView(convertView))
{
view = convertView;
}
if (view == null)
{
view = inflater.inflate(R.layout.help_list_item, parent, false);
if (view == null)
return null;
}
}
return view;
}
private boolean isLastView(View view)
{
if (view != null && view.findViewById(R.id.help_list_item_last) != null)
{
return true;
}
return false;
}
为了更灵活,代码可以不再使用xml布局,并在getView()代码中动态设置每个列表的样式。在我的例子中,使用2种不同的布局很简单。