不同外观的相同布局。为什么?

时间:2015-01-28 12:13:16

标签: android layout settings

以下图片显示了用于编辑游戏配置文件的首选项屏幕。 最小玩家数量为2,但用户可以通过点击“添加新玩家...”项目添加更多玩家(参见图2)。

Picture 1 enter image description here

也可以通过单击项目右侧的垃圾桶图标再次删除所有其他玩家。为了使trashcan-icon能够正常工作,我不得不稍微修改系统提供的名为preference.xml的首选项的布局。我用于动态添加的首选项的修改后的布局现在看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize"
    android:background="?android:attr/selectableItemBackground" 
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="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_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </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_vertical"
        android:orientation="vertical" />

</LinearLayout>

正如您所看到的,这与原始系统布局之间的唯一区别是附加属性android:descendantFocusability="blocksDescendants", 所有风格的具体定义都是一样的。 为什么增加的偏好项目在风格上与其他项目相比有所不同?看标题,它更大,图标的位置有点不同。

我真的不明白为什么会这样,你有解释吗?

这是代码,我在其中添加了一个新的首选项:

    @Override
    public boolean onPreferenceClick(final Preference preference) {

        final Context context = preference.getContext();
        final Preference newPref = new RemovablePreference(context);
        final PreferenceCategory playerCategory = (PreferenceCategory) preference
                .getPreferenceManager().findPreference("prof_key_players");

        final int count = playerCategory.getPreferenceCount();
        String title = String.valueOf(count) + ". Player";
        newPref.setTitle(title);
        final String summary = "Player" + String.valueOf(count);
        newPref.setIcon(R.drawable.player_green);
        newPref.setSummary(summary);
        newPref.setLayoutResource(R.layout.preference);
        newPref.setWidgetLayoutResource(R.layout.view_profile_remove);
        newPref.setOrder(count);

        playerCategory.addPreference(newPref);

        title = "Players (" + String.valueOf(count) + ")";
        playerCategory.setTitle(title);

        Toast.makeText(context, "New player added ...", Toast.LENGTH_SHORT)
                .show();

        return true;
    }

0 个答案:

没有答案
相关问题