2012-06-01 279 views
0

我已经设计了一个XML布局在此图像中:表格布局对齐?

the four buttons in table layout

我需要同样放置四个按钮在屏幕上,但我无法得到它的工作。我尝试了所有可能的改变,我可以想到。以下是布局的XML:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="40dp" 
    android:id="@+id/buttonlayout" 
    android:layout_alignBottom="@id/framelayout"> 


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

<TableRow> 

    <Button 
    android:id="@+id/previous" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/previous_icon" /> 

<Button 
    android:id="@+id/button_startprint" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/print_icon" 
    android:padding="10dp" /> 

<Button 
    android:id="@+id/button_cam" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/cam_icon" 
    android:padding="10dp" /> 

<Button 
    android:id="@+id/next" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/next_icon" 
    android:hapticFeedbackEnabled="true"/> 

</TableRow> 
</TableLayout> 
</RelativeLayout> 

任何人都可以帮我解决这个问题吗?

+1

喜@ jxgn你想在做垂直???? – itsrajesh4uguys

+0

nope。横向 – jxgn

+0

hi @ jxgn我已添加编辑中的代码..检查出来..希望它会帮助你。 – itsrajesh4uguys

回答

1

嗨jxgn使用此代码:

<?xml version="1.0" encoding="UTF-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/previous" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/ic_launcher" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/button_startprint" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/ic_launcher" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/button_cam" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/ic_launcher" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/next" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/ic_launcher" /> 
     </LinearLayout> 
    </TableRow> 

</TableLayout> 
+0

谢谢:)诀窍:) – jxgn

+0

你总是欢迎....... – itsrajesh4uguys

1

使用android:layout_weight=1,并使用android:layout_width="0dp",使重量相等的按钮..

编辑 例如定义按钮

<Button 
     android:id="@+id/button_cam" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=1 
     android:background="@drawable/cam_icon" 
     android:padding="10dp" /> 
+0

谢谢:)但图像被拉伸。有什么我可以做的吗? – jxgn

1

如果设置该属性机器人:体重=“1 “对于所有四个按钮,它们应该正确分开,因为TableRow是LinearLayout的子类。为了获得更好的性能,建议使用android:layout_width =“0dp”。

0

尝试设置在每个按钮:

android:layout_weight="0.25" 

希望这会已经为你工作

0

试试这个它是有用的

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/buttonlayout"  > 


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

<TableRow> 

    <Button 
    android:id="@+id/previous" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher" 
    android:layout_weight="1"/> 

<Button 
    android:id="@+id/button_startprint" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher" 
    android:layout_weight="1" 
    /> 

<Button 
    android:id="@+id/button_cam" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher" 
    android:layout_weight="1"  /> 

<Button 
    android:id="@+id/next" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher" 
    android:layout_weight="1"  /> 

    </TableRow> 
    </TableLayout> 
    </RelativeLayout> 
1

尝试更换所有的按钮与layout_width="0dp"宽并添加layout_weight="1"

尝试使用9贴片图像回避的事实,你的图像被拉伸

<TableRow> 

     <Button 
     android:id="@+id/previous" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/ic_menu_compass" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/button_startprint" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/ic_menu_mylocation" 
     android:padding="10dp" 
     android:layout_weight="1"/> 

    <Button 
     android:id="@+id/button_cam" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/ic_menu_mapmode" 
     android:padding="10dp" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/next" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/ic_menu_about" 
     android:hapticFeedbackEnabled="true" 
     android:layout_weight="1"/> 

</TableRow> 
0

在你的XML应用android:layout_weight="1"将舒展你的按钮图像。

如果你不想伸展按钮图像使用ImageButton并将android:src设置为您的drawables。

请参阅下面的代码:

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

    <TableRow> 

     <ImageButton 
      android:id="@+id/previous" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/previous_icon" 
      android:background="@android:color/transparent" 
      android:layout_weight="1" /> 

     <ImageButton 
      android:id="@+id/button_startprint" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/print_icon" 
      android:background="@android:color/transparent" 
      android:padding="10dp" 
      android:layout_weight="1" /> 

     <ImageButton 
      android:id="@+id/button_cam" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/cam_icon" 
      android:background="@android:color/transparent" 
      android:padding="10dp" 
      android:layout_weight="1" /> 

     <ImageButton 
      android:id="@+id/next" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/next_icon" 
      android:background="@android:color/transparent" 
      android:hapticFeedbackEnabled="true" 
      android:layout_weight="1" /> 
    </TableRow> 
</TableLayout> 

`