左右对齐图像

时间:2013-01-31 09:51:05

标签: android android-layout

我有一个listView,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

但我想要左边和右边的图像。我怎样才能得到这种效果?

7 个答案:

答案 0 :(得分: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 = true。

答案 1 :(得分:1)

在第一个linearlayout中写下“fill_parent”,如下所示:

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

答案 2 :(得分: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>

尝试上面的代码,你想要朋友

答案 3 :(得分: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>

答案 4 :(得分:1)

将其放入ImageButtun标签:

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"

答案 5 :(得分:0)

父LinearLayout具有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>

答案 6 :(得分: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>