首页 > 解决方案 > Android studio 横向布局的烦恼

问题描述

所以,我试图得到这样的东西。

在此处输入图像描述

我在将+-按钮排列在同一行时遇到问题。

一旦我有水平布局,我拖动按钮。我结束了这个。我不知道如何使它工作,从代码或设计窗口。请帮忙。

在此处输入图像描述

有什么解决办法吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/suma"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="171dp"
            android:layout_weight="1"
            android:onClick="incrementaContador"
            android:text="+" />

        <Button
            android:id="@+id/resta"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="169dp"
            android:layout_weight="1"
            android:onClick="restaContador"
            android:text="-" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/titulo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="Cafe del dia "
            android:textAlignment="center" />

        <TextView
            android:id="@+id/contadorA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="64dp"
            android:textAlignment="center"
            android:textSize="24sp"
            android:textStyle="bold" />
    </LinearLayout>

    <Button
        android:id="@+id/reinicio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Reinicio"
        android:onClick="reseteaContador" />
</RelativeLayout>

标签: androidandroid-layoutandroid-studiolayout

解决方案


你有

android:layout_alignParentTop="true" android:layout_marginTop="171dp"

一键式和

android:layout_alignParentTop="true" android:layout_marginBottom="169dp"

在另一。

尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

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

        <Button
            android:id="@+id/suma"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:onClick="incrementaContador"
            android:text="+" />


        <Button
            android:id="@+id/resta"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:onClick="restaContador"
            android:text="-" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/titulo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="Cafe del dia "
            android:textAlignment="center" />

        <TextView
            android:id="@+id/contadorA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="64dp"
            android:textAlignment="center"
            android:textSize="24sp"
            android:textStyle="bold" />
    </LinearLayout>

    <Button
        android:id="@+id/reinicio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Reinicio"
        android:onClick="reseteaContador"
        />


</RelativeLayout>

推荐阅读