假设我有一个布局xml,如下所示:
TableLayout
Row0: | textView_title (width=9) |
Row1: | ButtonX(width=3) | textView_describe (width=6) |
Row2: | ButtonY(width=3) | Button_back(width=3)| Button_start(width=3)|
水平合并可以使用span的技术,就像下面的编码一样。 我想问一下可以合并ButtonX和ButtonY吗? (即垂直?),如果没有,是否有任何方法可以进行这样的布局?
非常感谢!!
<?xml version="1.0" encoding="utf-8"?><TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:screenOrientation="landscape"
android:background="@drawable/blackboard" >
<TableRow
android:id="@+id/tableRow0"
android:layout_weight="0.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:background="@drawable/transparent_btn"
android:gravity="center"
android:layout_span="9"
android:text="@string/title_number"
android:textColor="@color/white"
android:textSize="60dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="0.5"
android:gravity="center" >
<Button
android:id="@+id/buttonX"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_span="3"
android:background="@drawable/transparent_btn"
android:text=""
android:textSize="30dp" />
<TextView
android:id="@+id/text_describe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_span="6"
android:background="@drawable/white_dot_btn"
android:gravity="top"
android:onClick="button_start_click"
android:text="abc"
android:textColor="@color/white"
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="0.3"
android:gravity="center" >
<Button
android:id="@+id/buttonY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_span="3"
android:background="@drawable/transparent_btn"
android:text=""
android:textSize="30dp" />
<Button
android:id="@+id/button_back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_span="3"
android:background="@drawable/white_btn"
android:text="quit"
android:onClick="button_back_click"
android:textSize="30dp" />
<Button
android:id="@+id/button_start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_span="3"
android:background="@drawable/green_btn"
android:text="start"
android:onClick="button_start_click"
android:textSize="30dp" />
</TableRow>
</TableLayout>
答案 0 :(得分:1)
我不认为用TableLayout
做到这一点。但你绝对可以用RelativeLayout
从代码到RelativeLayout
的快速转换:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:background="@drawable/transparent_btn"
android:gravity="center"
android:text="@string/title_number"
android:textColor="@color/white" />
<Button
android:id="@+id/buttonXY"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textview_title"
android:layout_margin="2dp"
android:background="@drawable/transparent_btn"
android:text="XY button" />
<TextView
android:id="@+id/text_description"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/buttonXY"
android:layout_margin="2dp"
android:layout_toRightOf="@+id/buttonXY"
android:background="@drawable/white_dot_btn"
android:gravity="top"
android:onClick="button_start_click"
android:text="abc"
android:textColor="@color/white" />
<Button
android:id="@+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/text_description"
android:layout_below="@+id/text_description"
android:layout_margin="2dp"
android:background="@drawable/white_btn"
android:onClick="button_back_click"
android:text="quit" />
<Button
android:id="@+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/buttonXY"
android:layout_alignParentRight="true"
android:layout_margin="2dp"
android:layout_toRightOf="@+id/button_back"
android:background="@drawable/green_btn"
android:onClick="button_start_click"
android:text="start" />
</RelativeLayout>
唯一的缺点就是宽度不再那么动态了。但是可能还有一些其他技巧可以用来解决这个问题。我把它留给你自己的灵感