什么布局最好用?

时间:2013-08-17 21:01:28

标签: java android android-layout

我想创建一个根据屏幕大小拉伸和占用空间的布局。我想在布局中显示以下内容。

enter image description here

最好的布局是什么?我想的可能是,顶部的线性布局和底部的相对布局,将其与父底部对齐。但是我如何确保它伸展到适合屏幕?

我可以使用layout_weight="1"进行线性布局以确保它占用屏幕空间,但高度又是什么?

2 个答案:

答案 0 :(得分:2)

在相对布局中,您只需设置使用android:layout_alignParentBottom = "true"android:layout_alignParentRight = "true"等,即可与屏幕对齐。

您可以使用android:layout_width = "match_parent"水平填充屏幕,或android:layout_height = "match_parent"垂直填充屏幕。您还可以指定某个金额,例如android:layout_width = "100dp"

我建议您使用android studio查看布局的实时预览,有很多帮助。

答案 1 :(得分:1)

为了使Drawing Cavas大于1/2,屏幕和其他组件变小,我认为你应该使用linear layout作为最外层布局,并使用layout_weight使画布大于其他。使用layout_weight时,您应该将子组件设为layout_height="0dp"。 这就是我提出的问题

<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:orientation="vertical" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="5dp"
    android:layout_weight="4"
    android:text="Canvas" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="5dp"
    android:layout_weight="1" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/button" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="5dp"
    android:layout_weight="2" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Image" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="50dp" /><!--Hard coded should be avoided. It depends on the screen size.-->
</RelativeLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
</LinearLayout>