3

我试图设置一个分隔线用于我的应用程序的列表中。我已做好“dicedivider”的XML代码如下图所示Android LinearLayout:分隔线不会显示

<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编码。任何想法,我要去哪里错了?

+0

见例如代码[* *这里**] [1],我也支持旧设备。 [1]:http://stackoverflow.com/questions/22547897/how-to-add-dividers-to-linearlayoutics/ –

+0

**不要忘记**的'机器人:showDividers'项! – Fattie

回答

-3

必须分频器设置到ListView,不给的LinearLayout

+1

没有ListView。只有一个LinearLayout。 据我所知,当我做这个代码时,ListView不会做我想在这里做的事情。这个LinearLayout包含一系列其他的LinearLayout,其中包含我需要的所有代码。当我在ListView上查找信息时,它只能包含另一个没有孩子的View。 LinearLayout存在分隔线方法。如果他们不工作,他们为什么在那里? – Jamstruth

0

您可以在XML使用

<View 
     android:layout_width="fill_parent" 
     android:layout_height="1dp"  android:Background="@android:color/darker_gray"/> 

要只是你的布局后置分频器

+0

这是我在解决了很多问题后所提出的解决方案(虽然我在Java代码中创建了视图,因为我需要动态添加它们)。虽然实际的android代码不起作用,但真是令人遗憾。它会更优雅。 – Jamstruth

+0

为什么不通过代码动态添加此视图?你创建一个线性布局,然后在该线性之后添加这个视图。这不是你的解决方案吗? –

0

尝试使用shape ="rectangle"代替line

<shape 
    android:shape="rectangle"> 

    <size android:height="1px" /> 
    <solid android:color="@color/white" /> 

</shape> 
0

创建内部RES /可绘制文件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>