android新功能:需要将按钮水平排列
我需要水平排列按钮,但是,我的按钮是垂直显示的。它们都叠在一起。。你知道我怎样才能做到这一点吗android新功能:需要将按钮水平排列,android,Android,我需要水平排列按钮,但是,我的按钮是垂直显示的。它们都叠在一起。。你知道我怎样才能做到这一点吗 <TableLayout android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="88dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="t
<TableLayout
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="88dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
</TableLayout>
使用线性布局,您可以轻松地在水平方向上表示按钮
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
</LinearLayout>
您尚未为您的帐户标识一个。想象一下如何创建一个表,行水平移动,列垂直移动
修复你的代码看起来像这样
<TableLayout
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="88dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
</TableRow>
</TableLayout>
或者,为了使事情更简单,我建议使用方向设置为水平的 使用线性布局,如下所示:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#646464"
android:gravity="center|center_vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<Button
android:id="@+id/inner_artist_profile"
android:layout_width="60dp"
android:layout_height="25dp"
android:text="Profile"
android:textColor="#AEAEAE"
android:background="@drawable/sub_nav_left" />
<Button
android:id="@+id/inner_artist_news"
android:layout_width="60dp"
android:layout_height="25dp"
android:text="News"
android:textColor="#AEAEAE"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/inner_artist_profile"
android:background="@drawable/sub_nav_middle" />
<Button
android:id="@+id/inner_artist_events"
android:layout_width="60dp"
android:layout_height="25dp"
android:text="Events"
android:textColor="#AEAEAE"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/inner_artist_news"
android:background="@drawable/sub_nav_middle" />
<Button
android:id="@+id/inner_artist_videos"
android:layout_width="60dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/inner_artist_events"
android:background="@drawable/sub_nav_middle"
android:text="Videos"
android:textColor="#AEAEAE" />
<Button
android:id="@+id/inner_artist_albums"
android:layout_width="60dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/inner_artist_videos"
android:background="@drawable/sub_nav_right"
android:text="Albums"
android:textColor="#AEAEAE" />
</RelativeLayout>
这来自一个工作项目,根据需要修改id,或者将
按钮
对象封装在表格行
标记中,例如
<TableRow>
<Button></Button>
</TableRow>
我认为应该使用带有权重和的线性布局,而不是表格布局。要均匀分布4个按钮的宽度,请将其宽度设置为0,将其权重设置为1。然后将LinearLayout的权重和设置为4(按钮数)。像这样:
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="88dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
有很多方法可以做到这一点。请检查以下方法之一
<LinearLayout 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"
android:weightSum="4" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
将TableRow添加到TableLayout,下面是工作代码
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="88dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" >
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
<Button android:layout_weight="1" />
</TableRow>
</TableLayout>
使用线性布局时,请确保再次将方向设置为水平。但这会影响我在屏幕上的视图代码,因此我无法进行此线性布局。我尝试按照mohammad的建议进行线性布局,我的所有按钮都不显示。实际上,你不需要线性布局的weightSum属性,但是按钮的重量特性是必需的。