【问题标题】:Android Layout beneath Action Bar操作栏下方的 Android 布局
【发布时间】:2020-07-13 18:46:09
【问题描述】:

我想制作一个带有多个按钮的屏幕,但是,操作栏下的按钮总是出现类似这样的内容。Wrong Buttons 这些按钮稍后以编程方式添加到LinearLayout

xml 文件看起来像 activity_main.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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"
    android:id="@+id/coordinator_layout"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize">

    </FrameLayout>

    <include
        layout="@layout/content_main"
        android:layout_width="331dp"
        android:layout_height="672dp"
        app:layout_anchor="@+id/container"
        app:layout_anchorGravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml

<androidx.constraintlayout.widget.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"
    android:id="@+id/mainScreen"
    tools:context=".MainActivity"
    >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="415dp"
        android:layout_height="606dp"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/button"
        android:layout_width="108dp"
        android:layout_height="86dp"
        android:layout_marginBottom="2dp"
        android:background="@android:drawable/ic_input_add"
        android:onClick="newScheme"
        android:tag="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.997"
        tools:ignore="OnClick" />


</androidx.constraintlayout.widget.ConstraintLayout>

如何获取操作栏下方而不是其下方的按钮?

【问题讨论】:

  • 可以添加content_main文件吗?
  • 我添加了content_main文件
  • 好的,你在content_main linearLayout添加按钮?
  • 是的,按钮是通过编程方式添加的。

标签: android xml layout android-actionbar


【解决方案1】:

我创建了类似的东西,但我更改了一些硬编码的大小(我不知道你做了什么,但我认为通常这不是一个好习惯)。

主要 XML

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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"
    android:id="@+id/coordinator_layout"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <include
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_anchor="@+id/container"
        app:layout_anchorGravity="center" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.XML

<androidx.constraintlayout.widget.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:id="@+id/mainScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".MainActivity">

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

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

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

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

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="108dp"
        android:layout_height="86dp"
        android:background="@android:drawable/ic_input_add"
        android:onClick="newScheme"
        android:tag="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="OnClick" />

</androidx.constraintlayout.widget.ConstraintLayout>

我在这里添加了按钮,但如果您以编程方式添加它,只需将其删除。

还有一个效果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多