如何实现理想的Android布局
我希望布局如下所示: 我知道这必须使用权重和线性布局、表格布局或网格布局的组合来完成 关于图像,除非另有规定,否则所有项目均水平居中 非常感谢您的帮助 我发现很难将占屏幕百分比的项目转换为权重。从我到目前为止所读到的内容来看,我似乎需要多层嵌套不同的布局,但我希望通过GridLayout功能可以轻松完成 这就是我到目前为止所做的:如何实现理想的Android布局,android,android-layout,Android,Android Layout,我希望布局如下所示: 我知道这必须使用权重和线性布局、表格布局或网格布局的组合来完成 关于图像,除非另有规定,否则所有项目均水平居中 非常感谢您的帮助 我发现很难将占屏幕百分比的项目转换为权重。从我到目前为止所读到的内容来看,我似乎需要多层嵌套不同的布局,但我希望通过GridLayout功能可以轻松完成 这就是我到目前为止所做的: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:
<?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:background="@drawable/bg_repeat"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal" >
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8"
android:contentDescription="@string/c_d"
android:src="@drawable/img" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" >
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8"
android:orientation="vertical" >
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:ems="10"
android:inputType="text" >
</EditText>
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="1dp"
android:clickable="true"
android:text="@string/btn" >
</Button>
</LinearLayout>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1" >
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:text="@string/btn2" >
</Button>
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:text="@string/btn2" >
</Button>
</LinearLayout>
</LinearLayout>
我将这两个图像合并为1。如果可以更有效地完成这项工作,我很想听听。最简单的方法就是自己编写布局。类似于从ViewGroup继承并实现onLayout()。如果有大量的线性布局,您的xml将像一团乱麻。在您的示例中,onLayout()将像这样,变量“bigImageView”和“smallImageView”在哪里初始化?我应该在XML文件中初始化它们还是在视图中创建它们?尝试使用layout_margin作为间距,并为imageview B设置线性布局的背景图像。在间距、权重和边距之间,您应该能够获得所需的外观。@Billshaped,这其实并不重要。但最方便的方法是使用“merge”根在xml中定义它们。然后,在自定义布局的构造器中,只需将它们充气即可。更多GIST顺便说一句