首页 > 解决方案 > 为什么按钮不稳定

问题描述

为什么按钮不稳定

从下图可以看出,为什么其他手机的按钮在不同的地方

你能帮我解决这个问题吗

我希望每部手机上的按钮都位于同一个位置。(如第一张图片)

我的手机

在此处输入图像描述

我的朋友电话(huwai)

在此处输入图像描述

我的朋友电话(三星)

在此处输入图像描述

我使用的代码如下。如何更改这些代码才能解决此问题?我对编码了解不多

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/v"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/y"
        android:text="Değiştir"
        android:textColor="#ffffff"
        android:layout_marginStart="20dp"
        android:layout_marginBottom="60dp"
        android:textSize="15dp"
        android:onClick="random"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/y"
        android:layout_marginBottom="60dp"
        android:layout_marginEnd="20dp"
        android:text="Kopyala"
        android:textColor="#ffffff"
        android:onClick="kopyala"
        android:textSize="15dp" />

    <Button
        android:text="Paylaş"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/y"
        android:layout_marginBottom="60dp"
        android:layout_marginEnd="140dp"
        android:textColor="#ffffff"
        android:textSize="15dp"
        android:id="@+id/button"
        android:onClick="paylas"/>

    
    
</RelativeLayout>

标签: javaandroidandroid-relativelayout

解决方案


尝试在水平方向的线性布局中添加权重(将视图彼此相邻放置)

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

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

            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button 1"
                android:textColor="#ffffff"
                android:textSize="15dp" />


            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button 2"
                android:textColor="#ffffff"
                android:textSize="15dp" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button 3"
                android:textColor="#ffffff"
                android:textSize="15dp" />


        </LinearLayout>
        
    </RelativeLayout> 

推荐阅读