【问题标题】:why the buttons are not stable为什么按钮不稳定
【发布时间】:2021-02-10 20:24:47
【问题描述】:

为什么按钮不稳定

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

你能帮我解决这个问题吗

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

我的手机

enter image description here

我朋友的电话(huwai)

enter image description here

我朋友的手机(三星)

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>

【问题讨论】:

  • 使用约束布局而不是相对布局

标签: java android android-relativelayout


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
        <?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>
    

    用这个改变你的布局,然后检查。

    【讨论】:

    • 我想这解决了我的问题。我的朋友们在睡觉。我以后也会试试的。我在另一部手机上试了一下,问题就解决了。谢谢我的朋友
    • 请考虑解释你的解决方案,而不是仅仅说try this,如果你解释你做了什么,至少人们会学到一些东西
    • 使用这三个按钮的布局权重来获得按钮的确切大小,并使按钮宽度 match_parent 和文本大小应该在 'sp' 中,如 '15sp' 而不是 '15dp'。
    【解决方案3】:

    试试这个。

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

    【讨论】:

    • 请考虑解释你的解决方案,而不是仅仅说try this,如果你解释你做了什么,至少人们会学到一些东西
    • 使用这三个按钮的布局权重来获得按钮的确切大小,并使按钮宽度 match_parent 和文本大小应该在 'sp' 中,如 '15sp' 而不是 '15dp'。
    【解决方案4】:

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

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

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

    使用 实施 'com.intuit.sdp:sdp-android:1.0.6' 实现'com.intuit.ssp:ssp-android:1.0.5'。 该库有助于根据您运行它的手机的参数进行扩展。

    【讨论】:

      猜你喜欢
      • 2012-01-08
      • 2013-10-20
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 2011-01-08
      相关资源
      最近更新 更多