顶部对齐视图和底部按钮

时间:2018-01-05 09:12:29

标签: android android-constraintlayout

我需要实现以下观点

enter image description here

我尝试使用约束布局:

<android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toTopOf="@id/segmentedButton">
     </FrameLayout>

     <Buttons
                android:id="@+id/segmentedButton"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent">
     </Buttons>

</android.support.constraint.ConstraintLayout>

当有很多项目时效果很好,但当只有2-3项时,它会在中间显示它们。 此外,我尝试使用LinearLayout但没有运气。

4 个答案:

答案 0 :(得分:1)

您也可以使用RelativeLayout来获得所需的结果。通过使用以下代码,如果它们是2或3,则可以在顶部显示项目。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/segmentedButton"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

<Button
    android:id="@+id/segmentedButton"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

</RelativeLayout>

答案 1 :(得分:0)

尝试使用线性布局,权重属性应按下按钮以在父级结尾处对齐

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
       >
    </FrameLayout>

    <Buttons
        android:id="@+id/segmentedButton"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        >
    </Buttons>

</LinearLayout>

我在这里假设按钮代表你制作的自定义按钮

答案 2 :(得分:0)

只需将Top Constraint添加到FrameLayout,就可以解决您的问题。

你的最终版面将是这样的:

String[] command = { "cmd", "/C", "start", "/B", "test.bat" };
File path = new File("C:/Users/Me/Desktop/dir/");
Runtime.getRuntime().exec(command, null, path);

答案 3 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/segmentedFrame"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/segmentedButton"
        android:background="@drawable/bg_scan_edittext">
    </FrameLayout>

    <Button
        android:id="@+id/segmentedButton"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        app:layout_constraintTop_toBottomOf="@+id/segmentedFrame"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">
    </Button>

</android.support.constraint.ConstraintLayout>
相关问题