“理解”;布局图“单位重量”;在Android中
我想在我的应用程序设计中使用表格,但是与Android的XML布局中的HTML不同,您无法使用百分比设置某个内容的宽度。从谷歌上看,似乎我可以使用“布局权重”来实现相同的功能,只要值加起来是100(http://stackoverflow.com/questions/3629331/android-trying-to-understand-androidlayout-weight)我写了我的布局,它似乎工作得很好 下面是它的外观&XML文件:“理解”;布局图“单位重量”;在Android中,android,xml,layout,Android,Xml,Layout,我想在我的应用程序设计中使用表格,但是与Android的XML布局中的HTML不同,您无法使用百分比设置某个内容的宽度。从谷歌上看,似乎我可以使用“布局权重”来实现相同的功能,只要值加起来是100(http://stackoverflow.com/questions/3629331/android-trying-to-understand-androidlayout-weight)我写了我的布局,它似乎工作得很好 下面是它的外观&XML文件: 但是,当我将按钮添加到此布局时,它会断开,如下所
但是,当我将按钮添加到此布局时,它会断开,如下所示:
有人能解释一下为什么会这样吗?我只需要在10%、80%和10%的位置设置三行,然后两行占据最后一行宽度的50%。如果我理解正确,问题是按钮正在向上推父行
如果是这样,请尝试在按钮上使用android:layout\u height=“wrap\u content”。我真的不知道为什么这对
表格布局不起作用,我尝试了很多,但都没有成功。但是,它可以与“线性布局”配合使用,因此如果您可以与它们共存:
<?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:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp"></LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8"></LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp">
<Button
android:text="Button"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button
android:text="Button"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"></Button>
</LinearLayout>
这不起作用的原因是您试图给出TableRow大小,然后将其添加到内部内容的大小中。使用TableLayout应该让内容视图控制大小。您的问题是,您需要一种允许按钮水平共享空间的东西,而不受高度权重的影响
实际上,TableRow只是一个水平线性布局,因此在底部TableRow中有两个TableRow是多余的,只需将其替换为一个线性布局即可
如果不知道前两行将包含什么内容,很难说什么是最好的,但表格布局可能不是最适合您需要的。是的,这就是问题所在。当我使用android:layout\u height=“wrap\u content”时,按钮的大小是正确的,但底部的一行仍然与我的第二张图片中的大小相同,按钮从顶部悬挂。您是希望按钮位于底部,还是必须是整个屏幕的10pc?
<?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">
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="@+id/tablerow1"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="10">
</TableRow>
<TableRow
android:id="@+id/tablerow2"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="80">
</TableRow>
<TableRow
android:id="@+id/tablerow3"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="10">
<TableRow
android:id="@+id/tablerow4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="50">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Speak">
</Button>
</TableRow>
<TableRow
android:id="@+id/tablerow5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="50">
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Listen">
</Button>
</TableRow>
</TableRow>
</TableLayout>
</LinearLayout>
<?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:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp"></LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8"></LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp">
<Button
android:text="Button"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button
android:text="Button"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"></Button>
</LinearLayout>