2011-03-16 71 views
5

我有2個ImageButons這樣安卓:中心對齊ImageButtons

enter image description here

我想盡一切辦法讓它在屏幕中心正確對齊。作爲Android的新手,我沒有任何其他線索可以做什麼。我如何能實現我的屏幕

的定心這2個Imagebuttons的目標,這是我的佈局XML

<TableLayout android:id="@+id/TableLayout01" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:stretchColumns="1"> 
     <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

      <ImageButton android:id="@+id/btn_Show" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:text="Show" android:src="@drawable/cmd_result_show" 
       android:layout_gravity="right" android:background="@android:color/transparent" /> 

      <ImageButton android:id="@+id/btn_showAll" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:text="Detailed Show" android:layout_gravity="center" 
       android:src="@drawable/cmd_result_details" android:background="@android:color/transparent" /> 
     </TableRow> 
    </TableLayout> 
+0

請添加哪些人爲您工作的評論,或者您有任何問題......並且接受正確的答案。 – Mathew 2011-03-16 05:56:45

回答

6

我希望你有直接這些按鈕的圖像。所以這個設置text屬性是沒用的。使用RelativeLayout可以輕鬆開發這些屏幕。

下面我附上了代碼。如果有任何問題,請查看並添加評論。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:gravity="center_horizontal"> 

    <ImageButton android:id="@+id/btn_Show" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:background="@drawable/cmd_result_show" /> 

    <ImageButton android:id="@+id/btn_showAll" 
     android:layout_toRightOf="@id/btn_Show" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:background="@drawable/cmd_result_details" /> 
</RelativeLayout> 
+0

謝謝..完美。我在兩者之間添加了一個空白的TextView,以在這些圖像按鈕之間給出一些差距。謝謝 – 2011-03-16 06:01:44

+2

如果你想添加空間,不需要添加一個文本視圖...添加邊距屬性...這將是足夠的。爲您的第一個按鈕給像android:layout_marginRight =「30dp」...它會添加邊距,因此間距將增加... – Mathew 2011-03-16 06:06:22

+0

謝謝.. – 2011-03-16 07:06:31

0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:gravity="center"> 
    <TableLayout android:id="@+id/TableLayout01" 
    android:stretchColumns="1" android:layout_height="wrap_content" 
    android:layout_width="wrap_content" > 
    <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
     <ImageButton android:id="@+id/btn_Show" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:text="Show" android:src="@drawable/cmd_result_show" 
      android:layout_gravity="right" android:background="@android:color/transparent" /> 
     <ImageButton android:id="@+id/btn_showAll" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:text="Detailed Show" android:layout_gravity="center" 
      android:src="@drawable/cmd_result_details" android:background="@android:color/transparent" /> 
    </TableRow> 
</TableLayout> 

享受!

0

你已經把ImageButton放在了表格裏面。表格將始終佔據屏幕的第一層。你想按鈕中心對齊,但在頂部位置?