2012-07-17 24 views
1

所以我有一個問題位置在同一直線上的三個項目爲我的Android應用程序正確定位三個項目。起初讓我告訴你什麼,我有一個截圖:Android的佈局,我不能在同一行

Image Problem

我在這裏的問題是,我想兩個圖像按鈕是行之和爲最左邊和最右邊大文本留在中心。

我曾嘗試沒有成功相對佈局,我也試過安卓重力每個項目,但它也不起作用。

這裏是XML我的佈局:提前

<?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="fill_parent" 
     android:background="#ffffff" 
     android:orientation="vertical" > 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" > 

      <ImageButton 
       android:id="@+id/imageButton1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/settings" /> 


      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Large Text" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="@color/black" /> 

      <ImageButton 
       android:id="@+id/imageButton2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" /> 

     </LinearLayout> 

    </LinearLayout> 

謝謝!

+0

具有u試圖用設置圖像按鈕和其他意見的嚴重性? – sampathpremarathna 2012-07-17 08:25:18

+0

首先,您想要與哪個佈局一起使用?我會建議你兩個。 – 2012-07-17 08:26:30

+0

我確實已經嘗試過重力,只要它是「最佳實踐」之一,我就不會在乎它的佈局;)。感謝大家花時間回覆! – 2012-07-17 08:57:10

回答

3

請使用此代碼嘗試。

<?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="fill_parent" 
     android:background="#ffffff" 
     android:orientation="vertical" > 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" > 

      <ImageButton 
       android:id="@+id/imageButton1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/settings" /> 


      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Large Text" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="@color/black" 
       android:gravity="center"/> 

      <ImageButton 
       android:id="@+id/imageButton2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" /> 

     </LinearLayout> 

    </LinearLayout> 
+0

使用'機器人時,你應該把'layout_width = 「0dp」'!這工作完美!所以我所要做的就是將重量設置爲1以使其工作?你能否也請解釋爲什麼我應該把layout_width設置爲0dp? – fiddler 2012-07-17 08:27:24

+0

感謝: – 2012-07-17 08:54:55

+0

當你想通過重量來控制其寬度或高度時,將該值設置爲0dip並讓重量控制實際值。雖然我很確定0在這裏是任意的,但你可以放任何東西,但是放0會讓你的意圖更清晰。 – 2012-07-17 08:58:09