2013-01-31 3 views
0

У меня есть 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

Но мне бы хотелось, чтобы изображения слева и справа. Как я могу получить этот эффект?

+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" 
Смежные вопросы