我是Android开发的完全初学者,我正在尝试做一些我认为应该简单但似乎不是(对我而言)的事情。下面的令人敬畏的ascii艺术展示了我想要实现的布局:
---------------------
| |
| |
| |
| |
| |
| Main View |
| |
| |
| |
| |
| |
---------------------
||btn a| |btn b||
---------------------
整个布局应该填满屏幕,底行停靠在屏幕底部(重力?),顶行占据屏幕的其余部分,它应该是流动的(即如果设备方向改变了它看起来像这样:
---------------------------
| |
| |
| Main View |
| |
| |
---------------------------
||btn a| |btn b||
---------------------------
如果我在HTML中工作,可以使用TABLE元素,两个TR元素(第一行的行数为2)和第二行中的几个TD元素(第二行的对齐值设置为对)。 (对于阅读本文的网页用户,我会在实践中使用DIV元素和CSS进行布局。)
所以,我的问题是第一行填充可用高度。我可以编写一个简单的算法,在java代码中执行此操作,但更喜欢使用XML进行布局。有没有简单的方法来做到这一点?我的代码如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<WebView android:id="@+id/site"
android:layout_span="3" />
</TableRow>
<TableRow>
<Button android:id="@+id/back"
android:text="btn a" />
<View android:id="@+id/filler" />
<Button
aroid:id="@+id/forward"
android:text="btn b" />
</TableRow>
</TableLayout>
答案 0 :(得分:4)
重量是正确的选择,试试这个(经过测试)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/linearLayout2"
android:layout_weight="1">
<WebView android:layout_height="wrap_content"
android:id="@+id/site"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:layout_weight="2"
android:text="Button"
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></Button>
<Button android:layout_weight="2"
android:text="Button"
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></Button>
</LinearLayout>
</TableLayout>
答案 1 :(得分:0)
我没有测试过,但这应该在理论上有效。我一回到计算机就会调查它:)
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/btna"
android:layout_height="42dp"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="Button A""
/>
<Button
android:id="@+id/btnb"
android:layout_height="42dp"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="Button B"
/>
</LinearLayout>
</RelativeLayout>
答案 2 :(得分:0)
您可以尝试放弃TableLayout并在webview上设置权重:
<WebView
android:id="@+id/site"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
(**Button, filler View, Button go here**)
</LinearLayout>
我没有测试过这个,但它应该很接近。
答案 3 :(得分:0)
对我使用RelativeLayout似乎有点矫枉过正,并且layout_alignParentBottom
无法始终信任。
尝试:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView android:id="@+id/site"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btna"
android:layout_height="42dp"
android:layout_width="wrap_content"
android:text="Button A">
<View android:id="@+id/filler"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/btnb"
android:layout_height="42dp"
android:layout_width="wrap_content"
android:text="Button B"/>
</LinearLayout>
</LinearLayout>
您可以使用 LinearLayout 和layout_weight
做很多事情。