为什么按钮不稳定

why the buttons are not stable

为什么按键不稳定

从下面的图片可以看出,为什么其他phones

的按钮在不同的地方

你能帮我吗

我希望按钮在每个 phone 上都位于同一位置。 (喜欢第一张图)

我的phone

enter image description here

我的朋友们phone (huwai)

enter image description here

我的朋友们phone(三星)

enter image description here

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

<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>
    <?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"
    android:background="@color/black"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="3"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="60dp">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/cardview_shadow_start_color"
            android:text="Değiştir"
            android:textColor="#ffffff"
            android:textSize="15sp"
            android:onClick="random"
            android:layout_weight="1"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"/>

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/cardview_shadow_start_color"
            android:text="Kopyala"
            android:textColor="#ffffff"
            android:onClick="kopyala"
            android:textSize="15sp"
            android:layout_weight="1"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"/>

        <Button
            android:text="Paylaş"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/cardview_shadow_start_color"
            android:textColor="#ffffff"
            android:textSize="15sp"
            android:id="@+id/button"
            android:onClick="paylas"
            android:layout_weight="1"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"/>

    </LinearLayout>


</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> 

试试这个。

<LinearLayout
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"
android:orientation="horizontal"
android:weightSum="3">

<Button
    android:id="@+id/btn1"
    android:layout_width="0dp"
    android:layout_weight="1"
    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="0dp"
    android:layout_weight="1"
    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="0dp"
    android:layout_weight="1"
    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"/>



 </LinearLayout>

这是因为不同的屏幕有不同的屏幕尺寸和像素密度。 所以它在不同的设备上显示不同。

您可以做的几件事是。 给出开始结束边距,然后限制布局,使其在不同的 phone 中看起来相同。 不要包装内容,而是使用定义的长度和宽度来设置您想要的图片。

在(mdpi、xxdpi 等)您的可绘制对象中有不同版本的图像。

使用 实施 'com.intuit.sdp:sdp-android:1.0.6' 实施 'com.intuit.ssp:ssp-android:1.0.5'。 这个库有助于根据 phone 你 运行 它的参数进行扩展。