我正在尝试为我的应用设置要在列表中使用的分隔线。我已经为“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/