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>