我应该使用什么android布局?

时间:2018-01-23 14:52:21

标签: android android-layout

我希望在显示屏较小/较大时调整组件大小。顶部和底部有两个大按钮。 3个中心对齐的按钮(这些按钮应始终保持32 * 32尺寸)。

enter image description here

3 个答案:

答案 0 :(得分:2)

您正在寻找约束布局。

1)在您的情况下,您的根布局是LinearLayout(垂直)

2)添加3个您选择的布局并设置“layout_weight”以满足您的需求 (默认权重总和为1,所以要有3个宽度相同的部分,每个部分设置为0.33如果你想让顶部和底部占据屏幕的40%,请将top_ bottom和layout_weight设置为0.4,将中间部分设置为0.2

[...]
<LinearLayout
    android:layout_weight="0.33"
[...]

3)中心的布局应该是约束布局。

将中心元素对齐到屏幕中心,然后从那里到左右元素,从左右元素到屏幕边框。然后在父元素的顶部和底部绘制约束。请注意,对于不同的屏幕宽度,元素之间的间距无论如何都会有所不同。

要改变左右元素的间距,您可以尝试不同的屏幕尺寸并更改这些属性:

android:layout_marginStart="96dp"

如果您在中间部分使用按钮图标,则建议对不同的屏幕尺寸使用不同的尺寸和dpi。然后设置这些图像按钮的高度和宽度以包装内容。可以找到指南here

图片:enter image description here

示例代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
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">


<LinearLayout
    android:layout_weight="0.33"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimaryDark"></LinearLayout>

<android.support.constraint.ConstraintLayout
    android:layout_weight="0.33"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="96dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_home_black_24dp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageView3"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.495"
        app:srcCompat="@drawable/ic_dashboard_black_24dp" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="96dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_notifications_black_24dp" />

</android.support.constraint.ConstraintLayout>

<LinearLayout
    android:layout_weight="0.33"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimaryDark"></LinearLayout>

答案 1 :(得分:1)

可以使用两个嵌套的LinearLayout来执行此操作。

在第一个LinearLayout两个大按钮应该有layout_gravity = topbottomlayout_weight = 1,{{1} } = height0px = width。第一个布局也应该有match_parent = orientation

三个小按钮的第二个布局应该嵌套在两个大按钮之间,固定vertical = height32dp = width而不是{{1} }。

答案 2 :(得分:0)

您应该使用约束布局并将组件附加到布局,以便在布局大小更改时,组件的大小也会更改

相关问题