【问题标题】:My buttons and texts are showing in the wrong places on Android Studio 4.0我的按钮和文本在 Android Studio 4.0 上显示在错误的位置
【发布时间】:2021-01-06 02:41:32
【问题描述】:

您好,我正在练习可点击按钮,为此,我需要在此处重现设计: https://www.figma.com/file/pAx28mFg68qvYulJQcyMHE/Score-Tracker?node-id=0%3A1

但由于某种原因,虽然我相信我写的所有尺寸都是正确的,但我无法将按钮和零点定位在它们应该在的确切位置,我不知道为什么!

这是我的 xml 代码。你能帮我看看我设置的尺寸有什么问题吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/background">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/background"/>
            <TextView
                android:id="@+id/numberOfGoals"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="163dp"
                android:layout_marginTop="124dp"
                android:text="0"
                android:textColor="#FFFFFF"
                android:textSize="72sp"/>
            <TextView
                android:id="@+id/numberOfGoals2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="163dp"
                android:layout_alignTop="@id/Button2"
                android:layout_marginBottom="124dp"
                android:text="0"
                android:textColor="#FFFFFF"
                android:textSize="72sp"/>
            <Button
                android:id="@+id/Button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Goal"
                android:textColor="#FFFFFF"
                android:textSize="14sp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginTop="14dp"
                android:backgroundTint="#EB5757"
                android:layout_alignParentTop="true"/>
            <Button
                android:id="@+id/Button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Goal"
                android:backgroundTint="#EB5757"
                android:textColor="#FFFFFF"
                android:textSize="14sp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="14dp"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

【问题讨论】:

    标签: android xml android-studio figma


    【解决方案1】:

    这可能会有所帮助。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@drawable/background">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/background"/>
    
                <TextView
                    android:id="@+id/numberOfGoals"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/Button1"
                    android:layout_centerHorizontal="true"
                    android:text="0"
                    android:textColor="#FFFFFF"
                    android:textSize="72sp"/>
                <TextView
                    android:id="@+id/numberOfGoals2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@id/Button2"
                    android:layout_centerHorizontal="true"
                    android:text="0"
                    android:textColor="#FFFFFF"
                    android:textSize="72sp"/>
                <Button
                    android:id="@+id/Button1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Goal"
                    android:textColor="#FFFFFF"
                    android:textSize="14sp"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginTop="14dp"
                    android:backgroundTint="#EB5757"
                    android:layout_alignParentTop="true"/>
                <Button
                    android:id="@+id/Button2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Goal"
                    android:backgroundTint="#EB5757"
                    android:textColor="#FFFFFF"
                    android:textSize="14sp"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="14dp"
                    android:layout_alignParentBottom="true"/>
            </RelativeLayout>
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 感谢您的帮助,我只是更改了尺寸,看起来还不错(我以为我必须坚持使用 figma 中的数字,但事实证明我可以调整它们以适合我的模拟器屏幕) .但我也会尝试你的建议并检查结果。谢谢
    猜你喜欢
    • 2020-10-08
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2013-04-15
    • 2017-12-22
    • 2013-01-20
    相关资源
    最近更新 更多