Android 如何创建具有6个按钮(如windows互动程序)的布局
我正在尝试创建一个带有6个按钮的布局,这些按钮可以自动适应windows phone的屏幕大小。在代码中,我动态创建了6个按钮,2个代表行,但是按钮应该适合填充后一行的屏幕大小。我怎样才能继续Android 如何创建具有6个按钮(如windows互动程序)的布局,android,android-layout,android-intent,android-listview,Android,Android Layout,Android Intent,Android Listview,我正在尝试创建一个带有6个按钮的布局,这些按钮可以自动适应windows phone的屏幕大小。在代码中,我动态创建了6个按钮,2个代表行,但是按钮应该适合填充后一行的屏幕大小。我怎样才能继续 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pare
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
我想你应该看看我会使用一个垂直
线性布局
,它有三行与子对象相同的权重,每行是一个水平线性布局
,它有两个相同权重的子对象,这将确保整个区域被填充。对于六个按钮,性能不应该是一个问题
如果性能是一个问题,您可以将行设置为RelativeLayout
s,并使用支柱将其一分为二,然后根据该支柱定位两个子行
当我说一个昂首阔步的时候,我的意思是:
<View android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
三个LinearLayout
子项将具有:
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_width="0dip"
android:layout_height="match_parent"
按钮将具有:
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_width="0dip"
android:layout_height="match_parent"
正如您所注意到的,我们为应用权重的属性设置了0dip
(如果父对象是垂直方向,则在高度上;如果父对象是水平方向,则在宽度上),这些属性需要增大以填充空间
以下是完整的XML(按钮不包括可绘制内容,因此可以随意添加):
结果是:
使用GridLayout!这对于这种情况是完美的
<GridLayout
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_margin="0.5dp"
android:columnCount="2"
android:rowCount="3" >
<Button
android:id="@+id/b_1"
/>
<Button
android:id="@+id/b_2"
/>
<Button
android:id="@+id/b_3"
/>
<Button
android:id="@+id/b_4"
/>
<Button
android:id="@+id/b_5"
/>
<Button
android:id="@+id/b_6"
/>
</GridLayout>
我正在使用Android.v7
库。这个xml为我创建了填充整个屏幕的2列3行布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/lib/android.support.v7.widget.GridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:layout_margin="0.5dp"
app:columnCount="2"
app:rowCount="3"
app:useDefaultMargins="true">
<Button
android:id="@+id/b_1"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_row="0"
grid:layout_column="0"
android:text="Hellooo"/>
<Button
android:id="@+id/b_2"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_row="0"
grid:layout_column="1"
android:text="Hellooo"/>
<Button
android:id="@+id/b_3"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_row="1"
grid:layout_column="0"
android:text="Hellooo"/>
<Button
android:id="@+id/b_4"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_row="1"
grid:layout_column="1"
android:text="Hellooo"/>
<Button
android:id="@+id/b_5"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_row="2"
grid:layout_column="0"
android:text="Hellooo"/>
<Button
android:id="@+id/b_6"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_row="2"
grid:layout_column="1"
android:text="Hellooo"/>
</android.support.v7.widget.GridLayout>
我用xml代码编辑了我的问题。但是有了这段代码,我在屏幕上无法正确显示的按钮显示方面遇到了问题android:layout_width=“0dip”
是这些按钮的所有宽度所需要的。不过,您没有更改按钮,但是,LinearLayout
s.用您需要的高度和宽度更新答案。我在LinearLayout android:layout\u height=“0dip”上有一个错误“可疑大小:这将使视图不可见,应与布局重量一起使用”。否则将很难回答。我看不到任何内容,因为此代码给了我以下错误:“可疑大小:这将使视图不可见,应与LinearLayout上的布局重量一起使用android:layout\u height=“0dip”问题是由于缺少android:layout\u weight=“1”
三个子线性布局的属性。我在回答中发布了完整的XML。GridLayout是解决方案吗?Windows互动程序也有拆分单元格,GridView有什么帮助?