2013-01-31 38 views
0

我有一个列表视图中,LIST_ITEM具有左侧的ImageButton,中心一个TextView和在右一个的ImageButton:对准的图像的左和右

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center"> 
     <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center"> 
     <ImageButton android:id="@+id/parkingState" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="4dip" 
      android:layout_marginRight="4dip" 
      android:layout_marginLeft="4dip" 
      android:layout_gravity="center" 
      android:background="@null"/> 
     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:gravity="center"> 
      <TextView android:id="@+id/LblSubTitle" 
       android:gravity="center_horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textStyle="normal" 
       android:textColor="#444444" 
       android:textSize="12sp" /> 
     </LinearLayout> 
     <ImageButton android:id="@+id/parking_details" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:layout_marginRight="4dip" 
       android:layout_marginLeft="4dip" 
       android:layout_marginTop="4dip" 
       android:layout_marginBottom="4dip" 
       android:background="@null"/> 
     </LinearLayout> 
</LinearLayout> 

结果是:

enter image description here

但我希望左侧和右侧的图像。我怎样才能得到这个效果?

+0

顶级的LinearLayout =>宽度= match_parent – njzk2

回答

1

使用RelativeLayout代替LinearLayout上面的代码,然后你可以使用layout标签:

android:layout_alignParentLeft = "true"android:layout_alignParentRight = "true"

注意:居中TextView使用:android:layout_centerHorizontal="true"

<RelativeLayout bla bla 
bla bla > 
<ImageView bla bla 
    bla bla 
    android:layout_alignParentLeft="true" /> 

<TextView bla bla 
    bla bla 
    android:layout_centerHorizontal="true" /> 

<ImageView bla bla 
    bla bla 
    android:layout_alignParentRight="true" /> 
</RelativeLayout> 
+0

什么是NB的意思? – user1256477

+1

这只是一个说明:) – Houcine

1

使用的RelativeLayout和使用 -

<ImageButton android:id="@+id/parkingState" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="4dip" 
    android:layout_marginRight="4dip" 
    android:layout_marginLeft="4dip" 
    android:layout_gravity="center" 
    android:background="@null" 
    android:layout_alignParentLeft="true"/> 

同样alignParentRight =为其他的ImageButton如此。

1

写 “FILL_PARENT” 在你的第一个LinearLayout中是这样的:

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

    <ImageButton 
     android:id="@+id/parkingState" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_gravity="center" 
     android:layout_marginLeft="4dip" 
     android:layout_marginRight="4dip" 
     android:layout_marginTop="4dip" 
     android:background="@null" 
     android:contentDescription="@string/app_name" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/LblSubTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@+id/parking_details" 
     android:layout_toRightOf="@+id/parkingState" 
     android:gravity="center_vertical" 
     android:singleLine="false" 
     android:text="write something here to display in the text" 
     android:textColor="#444444" 
     android:textSize="12sp" 
     android:textStyle="normal" /> 

    <ImageButton 
     android:id="@+id/parking_details" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginBottom="4dip" 
     android:layout_marginLeft="4dip" 
     android:layout_marginRight="4dip" 
     android:layout_marginTop="4dip" 
     android:background="@null" 
     android:contentDescription="@string/app_name" 
     android:gravity="center" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 

尝试要朋友

+0

朋友的工作尝试这些代码朋友 –

0

父LinerLayout有android:layout_width="wrap_content"。将其更改为fill_parent

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

</LinearLayout> 
0

添加线性布局,如下图所示...

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

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <ImageButton android:id="@+id/parkingState" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="4dip" 
     android:layout_marginRight="4dip" 
     android:layout_marginLeft="4dip" 
     android:layout_gravity="center" 
     android:background="@drawable/ic_launcher"/> 


    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center" 
     > 
     <TextView android:id="@+id/LblSubTitle" 
      android:gravity="center_horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="normal" 
      android:text="hhhh" 
      android:textColor="#444444" 
      android:textSize="12sp" /> 

    </LinearLayout> 

    <ImageButton android:id="@+id/parking_details" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_marginRight="4dip" 
     android:layout_marginLeft="4dip" 
     android:layout_marginTop="4dip" 
     android:layout_marginBottom="4dip" 
     android:background="@drawable/ic_launcher"/> 
</LinearLayout> 

</LinearLayout> 
1

要把它放到ImageButtun标签:

android:layout_alignParentLeft="true" 

android:layout_alignParentRight="true"