添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

如何在android studio的线性布局中把按钮放在彼此旁边?

0 人关注

你好,我想在android studio中用线性布局把两个按钮放在一起。

<LinearLayout 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="222dp"
    android:layout_height="237dp"
    app:srcCompat="@drawable/ic_launcher_background"
    tools:layout_editor_absoluteX="246dp"
    tools:layout_editor_absoluteY="322dp" />
<com.google.android.material.button.MaterialButton
    android:id="@+id/cameraBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/camera"
    android:layout_marginTop="8dp"
    app:strokeColor="@color/teal_700"
    app:cornerRadius="100dp"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"/>
<com.google.android.material.button.MaterialButton
    android:id="@+id/galleryBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Gallery"
    android:layout_marginTop="8dp"
    app:cornerRadius="100dp"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"

the output currently is:

我试着用相对布局--没有用,也用线性布局中的水平布局。

我试着把android:layout_weight也设为1,结果按钮消失了。

我想把imageview放在中间,下面有两个按钮,两个按钮挨在一起。

2 个评论
嗨,也许父母的宽度需要调整以适应?
是否必须使用线性布局?
android
android-studio
android-linearlayout
eyal haimov
eyal haimov
发布于 2022-01-26
5 个回答
Kamal Nayan
Kamal Nayan
发布于 2022-06-28
已采纳
0 人赞同

你可以在父布局内使用另一个 Linear Layout ,并将其方向设置为水平,以实现预期的输出。

Code For Desired Output:

<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="222dp"
    android:layout_height="237dp"
    app:srcCompat="@drawable/ic_launcher_background"
    tools:layout_editor_absoluteX="246dp"
    tools:layout_editor_absoluteY="322dp" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center"
    android:gravity="center">
    <com.google.android.material.button.MaterialButton
        android:id="@+id/cameraBtn"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/camera"
        app:cornerRadius="100dp"
        app:strokeColor="@color/teal_700" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/galleryBtn"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:text="Gallery"
        app:cornerRadius="100dp" />
</LinearLayout>
    
请为该答案投上一票,因为它可以在将来帮助其他人。
Filip Petrovski
Filip Petrovski
发布于 2022-06-28
0 人赞同

在你当前的布局中,所有的元素都在一个容器中,这就是你的LinearLayout。

用LinearLayout实现你想要的东西的一个想法是在两个不同的LinearLayout中分离图像和按钮。 将承载你的按钮的LinearLayout的方向应该是水平的。

<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="222dp"
        android:layout_height="237dp"
        app:srcCompat="@drawable/ic_launcher_background"
        tools:layout_editor_absoluteX="246dp"
        tools:layout_editor_absoluteY="322dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <com.google.android.material.button.MaterialButton
            android:id="@+id/cameraBtn"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:text="@string/camera"
            app:cornerRadius="100dp"
            app:strokeColor="@color/teal_700" />
        <com.google.android.material.button.MaterialButton
            android:id="@+id/galleryBtn"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Gallery"
            app:cornerRadius="100dp" />
    </linearLayout>
</LinearLayout>

如果LinearLayout不是一个硬性要求,我建议使用ConstraintLayout。通过使用它,你可以以一种更好、更简单的方式定位所有的东西。

非常感谢,我将尝试使用ConstraintLayout。
你也可以使用线性的,它将根据设备的大小适合于每个设备。
JAY_Panchal
JAY_Panchal
发布于 2022-06-28
0 人赞同

你需要使用另一个线性布局,把两个按钮放在彼此旁边。 以下是这方面的代码。

<LinearLayout 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:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="222dp"
        android:layout_height="237dp"
        app:srcCompat="@drawable/ic_launcher_background"
        tools:layout_editor_absoluteX="246dp"
        tools:layout_editor_absoluteY="322dp" />
    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.google.android.material.button.MaterialButton
            android:id="@+id/cameraBtn"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Camera"
            app:cornerRadius="100dp"
            app:strokeColor="@color/teal_700" />
        <com.google.android.material.button.MaterialButton
            android:id="@+id/galleryBtn"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:text="Gallery"
            app:cornerRadius="100dp" />
    </androidx.appcompat.widget.LinearLayoutCompat>
</LinearLayout>

以下是所需输出的截图。 你可以在布局中给按钮设置所需的边距。

DevinM
DevinM
发布于 2022-06-28
0 人赞同

如果你打算把这个视图放在LinearLayout里面,你可以这样做。你将在ImageView下面添加一个新的LinearLayout,并设置其方向为水平。然后在这个LinearLayout中添加你的按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="222dp"
        android:layout_height="237dp"
        app:srcCompat="@drawable/ic_launcher_background" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.google.android.material.button.MaterialButton
            android:id="@+id/cameraBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/camera"
            android:layout_marginTop="8dp"
            app:strokeColor="@color/teal_700"
            app:cornerRadius="100dp"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"/>
        <com.google.android.material.button.MaterialButton
            android:id="@+id/galleryBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gallery"
            android:layout_marginTop="8dp"
            app:cornerRadius="100dp"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    </LinearLayout>
</LinearLayout>

如果你对使用ConstraintLayout感兴趣,你可以这样做。

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="222dp"
        android:layout_height="237dp"
        app:srcCompat="@drawable/ic_launcher_background"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/cameraBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/camera"
        android:layout_marginTop="8dp"
        app:strokeColor="@color/teal_700"
        app:cornerRadius="100dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintEnd_toStartOf="@+id/galleryBtn"
        app:layout_constraintStart_toStartOf="parent"/>
    <com.google.android.material.button.MaterialButton
        android:id="@+id/galleryBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gallery"
        android:layout_marginTop="8dp"
        app:cornerRadius="100dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintStart_toEndOf="@+id/cameraBtn"
        app:layout_constraintEnd_toEndOf="parent"/>