Java 使桌行宽度均匀
下面是我的屏幕的外观: 您可以看到,在第1行中,两个按钮没有均匀对齐,因为右框为2行,左框为1行。此外,您可以在第3行中看到按钮更宽,因为它们都是3行 有没有办法使两行的高度相同?有点像在Java 使桌行宽度均匀,java,android,width,tablelayout,tablerow,Java,Android,Width,Tablelayout,Tablerow,下面是我的屏幕的外观: 您可以看到,在第1行中,两个按钮没有均匀对齐,因为右框为2行,左框为1行。此外,您可以在第3行中看到按钮更宽,因为它们都是3行 有没有办法使两行的高度相同?有点像在LinearLayout中如何使用android:layout\u width=“#”。由于我的XML相对较短,因此我已将所有代码发布在XML中 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
LinearLayout
中如何使用android:layout\u width=“#”
。由于我的XML相对较短,因此我已将所有代码发布在XML中
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativelayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="1dp"
android:paddingBottom="50dp"
android:background="@drawable/scroll" >
<TextView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="14sp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:paddingTop="65dp" />
<TextView
android:id="@+id/subHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/header"
android:textStyle="italic|bold"
android:textSize="12sp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:paddingBottom="20dp" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/subHeader"
android:layout_alignParentBottom="true"
android:background="@drawable/scrollviewborder"
android:fillViewport="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/jc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="14sp" />
<Button
android:id="@+id/tencommandments"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/exodus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="14sp" />
<Button
android:id="@+id/genesis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/holydays"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="14sp" />
<Button
android:id="@+id/facts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/random"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="14sp" />
</TableRow>
</TableLayout>
</RelativeLayout>
尝试将android:layout\u height=“wrap\u content”更改为将按钮中的父项与idjc
匹配,这是您的布局,重做。你会想把你的抽屉里的线条加进去,我把它们拿了出来,因为我没有权限。注意权重的值是如何分数的,并考虑容器中小部件可用的空间量占该空间的百分比。这种范式转换应该提升您对权重属性的理解
另外,尽量避免使用fill\u parent
,这在API 8及以上版本中已被弃用,取而代之的是match\u parent
,除非您真的想支持旧的、发霉的Android版本(少于安装基数的1%)
这至少使第1行上的两个按钮高度相同。但所有4行的高度仍然不相同。我已经用新的代码和新的屏幕截图更新了我的开场白。@马特,因为每个按钮的文本都不同,不可避免地,在某些设备上,按钮的高度会有所不同,因为水平放置文本的空间会减少。如果希望每个设备上的高度完全相同,唯一的实际选项是将高度硬编码为某个dp值。因此,无法确定第3行将具有最高的高度,然后将第1、2和4行作为第3行的高度进行匹配?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativelayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="1dp"
android:paddingBottom="50dp" >
<TextView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="14sp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:paddingTop="65dp" />
<TextView
android:id="@+id/subHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/header"
android:textStyle="italic|bold"
android:textSize="12sp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:paddingBottom="20dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/subHeader"
android:layout_alignParentBottom="true"
android:fillViewport="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".25" >
<Button
android:id="@+id/jc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:textStyle="bold"
android:textSize="14sp" />
<Button
android:id="@+id/tencommandments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:textStyle="bold"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".25" >
<Button
android:id="@+id/exodus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:textStyle="bold"
android:textSize="14sp" />
<Button
android:id="@+id/genesis"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:textStyle="bold"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".25" >
<Button
android:id="@+id/holydays"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:textStyle="bold"
android:textSize="14sp" />
<Button
android:id="@+id/facts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:textStyle="bold"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".25" >
<Button
android:id="@+id/random"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="14sp" />
</TableRow>
</TableLayout>
</RelativeLayout>