2013-08-30 72 views
0

我對Android很新穎。我想在右下角的列表視圖項目模板中添加兩個按鈕。帶按鈕的Android ListViewItem佈局

現在我有這樣的佈局: existing layout

,代碼:

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="6dip" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/secondLine" 
    android:layout_width="fill_parent" 
    android:layout_height="26dip" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_toRightOf="@id/icon" 
    android:ellipsize="marquee" 
    android:singleLine="true" 
    android:text="Details" 
    android:textSize="12sp" /> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/secondLine" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignWithParentIfMissing="true" 
    android:layout_toRightOf="@id/icon" 
    android:gravity="center_vertical" 
    android:text="Test title" 
    android:textSize="16sp" /> 

我想實現的是添加兩個按鈕在右下角的玉米呃,是這樣的:

expected

我也試圖把RelativeLayoutLinearLayout再增加一個孩子LinearLayout與按鈕horizo​​ntaly對準它,但列表視圖項行不顯示它們。

+0

把代碼與父佈局.. –

+0

顯示一些代碼,請。 – ClaireG

+0

我在詢問您想要顯示按鈕的行佈局代碼 –

回答

-1

如果我理解你以及你需要的按鈕,每個列表項的右下角,您可以使用此代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="right" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" > 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/icon" 
     android:gravity="center_vertical" 
     android:text="Test title" 
     android:textSize="16sp" /> 

    <TextView 
     android:id="@+id/secondLine" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/title" 
     android:layout_toRightOf="@id/icon" 
     android:singleLine="true" 
     android:text="Details" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 
</RelativeLayout> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="#000000" 
    android:layout_marginLeft="2dp" /> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button1" /> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="match_parent" 
     android:background="#000000" 
     android:layout_marginLeft="2dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button2" /> 
</LinearLayout> 

修改代碼w.r.t您需要

0

。如果是這樣,下面會適合你:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/MainLayout" 
android:layout_width="fill_parent" 
android:layout_height= "fill_parent"> 
    <RelativeLayout 
     android:id="@+id/FirstLayout" 
     android:layout_width="fill_parent" 
     android:layout_height= "fill_parent"> 
      <ImageView 
       android:id="@+id/icon" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentTop="true" 
       android:layout_marginRight="6dip" 
       android:src="@drawable/ic_launcher" /> 

      <TextView 
       android:id="@+id/secondLine" 
       android:layout_width="fill_parent" 
       android:layout_height="26dip" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentRight="true" 
       android:layout_toRightOf="@id/icon" 
       android:ellipsize="marquee" 
       android:singleLine="true" 
       android:text="Details" 
       android:textSize="12sp" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_above="@id/secondLine" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true" 
       android:layout_alignWithParentIfMissing="true" 
       android:layout_toRightOf="@id/icon" 
       android:gravity="center_vertical" 
       android:text="Test title" 
       android:textSize="16sp" /> 
    </RelativeLayout> 
    <RelativeLayout 
     android:id="@+id/SecondLayout" 
     android:layout_below="@+id/FirstLayout" 
     android:layout_width="fill_parent" 
     android:layout_height= "fill_parent"> 
       <Button 
        android:id="@+id/btnTwo" 
        android:text ="Button Two" 
        android:layout_alignParentRight="true"/> 
       <Button 
        android:id="@+id/btnOne" 
        android:text ="Button One" 
        android:layout_toLeftOf="@+id/btnTwo"/> 
     </RelativeLayout> 
</RelativeLayout> 

祝你好運!

+0

請在發佈之前驗證您的代碼作爲答案。 –

+0

驗證.... – ClaireG

0

使用下面的代碼爲您所需的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#DADADA" > 

    <RelativeLayout 
     android:id="@+id/ItemMainContainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:padding="5dp" > 

     <ImageView 
      android:id="@+id/Icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/image" /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_toRightOf="@id/Icon" 
      android:orientation="vertical" > 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Test title" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Details" /> 
     </LinearLayout> 
    </RelativeLayout> 

    <View 
     android:id="@+id/HorizontalSeparator" 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_below="@id/ItemMainContainer" 
     android:layout_margin="5dp" 
     android:background="#CCCCCC" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/HorizontalSeparator" 
     android:layout_margin="2dp" > 

     <TextView 
      android:id="@+id/Button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_margin="5dp" 
      android:clickable="true" 
      android:drawableLeft="@drawable/delete" 
      android:gravity="center" 
      android:paddingLeft="15dp" 
      android:paddingRight="15dp" 
      android:text="Button2" /> 

     <View 
      android:id="@+id/Separator" 
      android:layout_width="1dp" 
      android:layout_height="27dp" 
      android:layout_toLeftOf="@id/Button2" 
      android:background="#CCCCCC" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:layout_toLeftOf="@id/Separator" 
      android:clickable="true" 
      android:drawableLeft="@drawable/delete" 
      android:gravity="center" 
      android:paddingLeft="15dp" 
      android:paddingRight="15dp" 
      android:text="Button1" /> 
    </RelativeLayout> 

</RelativeLayout> 

我還附上結果的截圖。 screenshot

相應地更改資產。

0

如果我沒有錯,您正在尋找組件的對齊調整。您已經使用android:layout_alignParentXXXXlayout_above /低於屬性。 請做一些改變,你會得到想要的。

我檢查了下面的代碼,這可能會對您有所幫助。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     android:id="@+id/rel2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/lin1" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" > 
// here place you existing code 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/lin1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" > 

     <Button 
      android:id="@+id/button1" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button2" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 
    </LinearLayout> 

</RelativeLayout> 
0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp"> 

    <ImageView 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:scaleType="fitXY" 
     android:adjustViewBounds="true" 
     android:src="@drawable/ic_launcher"/> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Title"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:text="Description"/> 
     </LinearLayout> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right|center_vertical"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:text="Button2"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:padding="5dp" 
     android:text="Button1"/> 
</LinearLayout> 
</LinearLayout>