2014-09-25 30 views
0

所以我有一個ExpandableListView,基本上我有一些文本(人的名字),然後我想要一個按鈕在同一行(如果可能的話朝向右側)。目前,這個代碼在我的XML文件中有一行文本,然後是下一行的按鈕,我玩過它,但無法弄清楚如何在一行上得到它。Android:在同一行上的TextView和Button在ExpandableListView中

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="55dip" 
android:orientation="horizontal" 
android:weightSum="1" > 

<TextView 
android:id="@+id/lblListItem" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="17dp" 
android:paddingTop="5dp" 
android:paddingBottom="5dp" 
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" /> 

<Button 
style="?android:attr/buttonStyleSmall" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:focusable="false" 
android:focusableInTouchMode="false" 
android:layout_weight="0.8" 
android:textSize="11dp" 
android:text="Checkout" 
android:id="@+id/checkout" /> 

</LinearLayout> 

回答

2

這將做到這一點。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="55dip" 
    android:orientation="horizintal" // Edited here 
    androi:weightSum="1" > // here 

<TextView 
    android:id="@+id/lblListItem" 
    android:layout_width="match_parent" //and here 
    android:layout_height="35dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:gravity="center_vertical" 
    android:layout_weight="0.2" // here 
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" /> 

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="match_parent" // and here 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:layout_weight="0.8" // and here 
    android:textSize="12dp" 
    android:text="Checkout" 
    android:id="@+id/checkout" /> 

</LinearLayout> 

使用weightsum

1時確認的兩件事情),如果方向是水平的,在所有子視圖寬度連續被持續顯示需要有「match_parent」和layout_weight一些價值。

2)0.2會給母體的水平寬度和0.8的80%將得到20%

+0

「的所有子視圖在一行被持續顯示將需要有‘match_parent’」通常它們是「0dp」 – codeMagic 2014-09-25 14:53:02

+0

是0dp會更有效率。因爲它會導致該屬性被忽略。 – 2014-09-25 15:00:16

+0

感謝您的幫助和其他人! – 2014-09-25 15:48:27

1

你在你父母0​​其堆疊從上到下(自然)元素具有

android:orientation="vertical" 

。刪除該行(因爲horizontalLinearLayout的默認方向)。然後,您需要使用weightmargin將其定位在您想要的位置。

另一種選擇是將LinearLayout替換爲RelativeLayout。這將允許您在Button上使用android:alignParentRight="true"將其放在屏幕的右側(RelativeLayoutViews置於默認的左上角)。

相關問題