我尝试以编程方式使用自定义布局实现首选项。我尝试更改它们未显示的视图组件。以下是优惠代码:
public class AvatarPreference extends Preference {
private String name;
private int drawableAvatarDefault;
public AvatarPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public AvatarPreference(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public AvatarPreference(Context context) {
super(context);
}
public AvatarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
TextView title = (TextView) view.findViewById(android.R.id.title);
icon.setImageResource(drawableAvatarDefault);
title.setText(name);
}
public void setName(String name) {
this.name = name;
}
public void setDrawableAvatarDefault(int drawableAvatarDefault) {
this.drawableAvatarDefault = drawableAvatarDefault;
}
这是设置首选项的代码:
AvatarPreference pref = new AvatarPreference(getActivity());
pref.setName(pkg.getNome());
pref.setDrawableAvatarDefault(pkg.getDrawableDefault());
pref.setLayoutResource(R.layout.pref_child);
这是自定义布局xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_item_match"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="16dip"
android:paddingRight="?android:attr/scrollbarSize" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+android:id/icon"
android:layout_width="@dimen/avatar_size"
android:layout_height="@dimen/avatar_size"
android:layout_marginRight="10dp"
android:layout_gravity="center" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_weight="1" >
<TextView
android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:textStyle="bold"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/title"
android:layout_below="@android:id/title"
android:maxLines="4"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" />
布局由图标和文本组成。当在方法中设置它们时,不显示onBindView。我也尝试重新定义方法getView,但我得到了相同的结果。我希望有一个人可以帮助我。感谢
编辑:这是settings.xml:
<PreferenceCategory android:title="@string/pref_cat_title_avatar" android:key="avatars">
<!-- This is the point where the preference is added progammaticamente -->
</PreferenceCategory>
这是创建偏好的完整方法:
private void installPreferenceAvatarPackage() {
AvatarManager.installPackage(getActivity());
PreferenceCategory category = (PreferenceCategory)findPreference("avatars");
category.removeAll();
List<PackageAvatar> packages = AvatarManager.getAvatarPackage();
for(PackageAvatar pkg : packages) {
AvatarPreference pref = new AvatarPreference(getActivity());
pref.setName(pkg.getNome());
pref.setDrawableAvatarDefault(pkg.getDrawableDefault());
pref.setLayoutResource(R.layout.pref_child);
category.addPreference(pref);
}
}
这是显示的内容:
在Avatar类别中出现ImageView和textView,它们被设置为onBindView AvatarPreference方法的一部分