Android 线性布局 : Dividers won't show

标签 android android-linearlayout android-drawable divider xml-drawable

我正在尝试为我的应用设置要在列表中使用的分隔线。我已经为“dicedivider”制作了 XML 代码,如下所示

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
   android:width="1px"
   android:color="@color/divider_Color"
   />

</shape>

然后我尝试将它设置为我的 LinearLayout 的可绘制分隔线,如下所示

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    diceCount = 0;
    diceList = (LinearLayout) this.findViewById(R.id.main_Dice_List);

    diceList.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
    diceList.setDividerDrawable(this.getResources().getDrawable(R.drawable.dicedivider));
    diceList.setDividerPadding(5);
    addDice();
}

尽管如此,应用程序仍未显示分隔线。我什至尝试过将它直接嵌入到 XML 中,但没有成功。

我是 Android 编码的新手。知道我哪里出错了吗?

最佳答案

在 res/drawable 中创建一个文件 mydivider.xml 并放置以下形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="1dip" />
    <solid android:color="#ffffff" />
</shape>

添加形状作为布局的分隔线

<LinearLayout android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:divider="@drawable/mydivider"
    android:showDividers="middle"
    android:dividerPadding="22dp">    
</LinearLayout>

关于Android 线性布局 : Dividers won't show,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18304130/

相关文章:

android - findViewById 在扩展的 LinearLayout 中返回 null

android - 如何将可绘制对象设置为 TextView 的文本?

android - 了解 Android 的 <layer-list>

android - 了解 Android 架构组件示例 GithubBrowserSample : ViewModelModule, ViewModel 参数

java - 如何获得 java_proto_library 的 1.7 兼容输出?

android - 无法从 Android 链中获取根 CA 证书

android - 社交(Google 和 Facebook)按钮未在水平 LinearLayout 中对齐

android - MvxBindableLinearLayout ItemsSource 绑定(bind)问题

android - 在 Android 上针对不同的屏幕尺寸和密度重复使用可绘制图像

android - 使用 Intent 将变量传递给 Zooper 小部件