Java android如何设计这种布局
我有一个示例布局,我正试图在android中使用xml创建它。我可以创建类似的布局,但我觉得我的方法可能是错误的 在这些情况下,我一直在做的是嵌套相对布局以充当“行”。下图展示了我将要做的事情 你们将如何创建类似于此的布局?我觉得嵌套相对布局有点过头了,但我不确定如果不这样做,我怎么能把所有东西都放在中心 当我没有嵌套我使用的布局时Java android如何设计这种布局,java,android,android-layout,Java,Android,Android Layout,我有一个示例布局,我正试图在android中使用xml创建它。我可以创建类似的布局,但我觉得我的方法可能是错误的 在这些情况下,我一直在做的是嵌套相对布局以充当“行”。下图展示了我将要做的事情 你们将如何创建类似于此的布局?我觉得嵌套相对布局有点过头了,但我不确定如果不这样做,我怎么能把所有东西都放在中心 当我没有嵌套我使用的布局时 android:layout_toRightOf="..." android:layout_below="@+id/t1" 在t5、t6和t7上(从图像)。结果
android:layout_toRightOf="..."
android:layout_below="@+id/t1"
在t5、t6和t7上(从图像)。结果似乎不正确。t1、t2、t3和t4不再水平居中
有没有一种方法可以告诉相对布局,在这一点之后的所有内容都应该显示在新行上?还是相对布局是这样做的错误方式?我不认为表格布局会正常工作,因为每一行不一定需要有相同数量的视图,而且它们需要居中
任何建议,谢谢 您可以在垂直
线性布局中嵌套两个水平线性布局
。可能不如相对性yout
那么有效,但更容易获得所需的居中行为
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G" />
</LinearLayout>
</LinearLayout>
您可以在垂直线性布局中嵌套两个水平线性布局
。可能不如相对性yout
那么有效,但更容易获得所需的居中行为
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G" />
</LinearLayout>
</LinearLayout>
看看这个:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="50dp"></LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/white"
android:layout_below="@+id/l1"
android:id="@+id/v1"/>
<LinearLayout
android:id="@+id/l2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/v1"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
android:id="@+id/pos"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
/>
<Button
android:id="@+id/neu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
<Button
android:id="@+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
<Button
android:id="@+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/white"
android:layout_below="@+id/l2"
android:id="@+id/v2"
/>
<LinearLayout
android:id="@+id/l3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/v2"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
android:id="@+id/pos"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
/>
<Button
android:id="@+id/neu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
<Button
android:id="@+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/white"
android:layout_below="@+id/l3"
android:id="@+id/v3"
/>
</RelativeLayout>
查看以下内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="50dp"></LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/white"
android:layout_below="@+id/l1"
android:id="@+id/v1"/>
<LinearLayout
android:id="@+id/l2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/v1"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
android:id="@+id/pos"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
/>
<Button
android:id="@+id/neu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
<Button
android:id="@+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
<Button
android:id="@+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/white"
android:layout_below="@+id/l2"
android:id="@+id/v2"
/>
<LinearLayout
android:id="@+id/l3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/v2"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
android:id="@+id/pos"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
/>
<Button
android:id="@+id/neu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
<Button
android:id="@+id/neg"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/white"
android:layout_below="@+id/l3"
android:id="@+id/v3"
/>
</RelativeLayout>
您也可以尝试改用线性布局。在第一个线性布局中,将权重和设置为4,将布局权重设置为1,使其等分,并将每个视图的重心设置为中心。
在发送线性布局中执行相同的操作。您也可以尝试使用线性布局。在第一个线性布局中,将权重和设置为4,将布局权重设置为1,使其等分,并将每个视图的重心设置为中心。
在发送线性布局中执行相同的操作。您也可以使用权重来尝试
<?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"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="2.5"
android:gravity="center"
android:text="hello" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="B" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="D" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="E" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="F" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="G" />
</LinearLayout>
</LinearLayout>
您也可以使用重量来尝试
<?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"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="2.5"
android:gravity="center"
android:text="hello" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="B" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="D" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="E" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="F" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="G" />
</LinearLayout>
</LinearLayout>
我不知道为什么有人否决了我的问题。一条让我知道原因的评论,我很乐意修正这个问题……我不知道为什么有人否决了我的问题。一个评论让我知道为什么,我会很高兴地解决这个问题…谢谢Sparky,我想嵌套布局可能是这种设计的正确方式!我想可能会有一种没有筑巢的更整洁的方法,但我想不会!谢谢我修改了我的答案,加入了一个没有嵌套的版本。我从嵌套版本开始,使用自动重构来帮助创建非嵌套版本。这正是我希望看到的:)我将对此进行调整,以了解它是如何工作的。我想我想要的是“布局基线”。再次感谢!谢谢Sparky,我想嵌套布局可能是这种设计的正确方式!我想可能会有一种没有筑巢的更整洁的方法,但我想不会!谢谢我修改了我的答案,加入了一个没有嵌套的版本。我从嵌套版本开始,使用自动重构来帮助创建非嵌套版本。这正是我希望看到的:)我将对此进行调整,以了解它是如何工作的。我想我想要的是“布局基线”。再次感谢!谢谢阿卡什。与Sparky的评论相同;)但是Sparky提供的和我提供给你的有很大区别。实现上面的代码,你会发现你的需求是什么。谢谢Hanks AkashG。与Sparky的评论相同;)但是Sparky提供的和我提供给你的有很大的不同。实现上面的代码,你会发现你的需求是什么。谢谢