Android studio doesn't draw button correctly

点点圈 提交于 2019-12-11 06:48:14

问题


In my Android studio project I have a fragment with constraint layout inside. Here is XML.

        <android.support.constraint.ConstraintLayout
            android:id="@+id/pinLayout"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/topLayout">

            <android.support.constraint.ConstraintLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:visibility="visible">

                <TextView
                    android:id="@+id/numberTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:gravity="top"
                    android:text="00000000"
                    android:textColor="@color/appTintColor"
                    android:textSize="18sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </android.support.constraint.ConstraintLayout>

            <android.support.constraint.ConstraintLayout
                android:id="@+id/constraintLayout2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.4"
                app:layout_constraintStart_toStartOf="parent">

                <Button
                    android:id="@+id/confirmButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/rounded_corner_shape_tint"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp"
                    android:text="Confirm Pin"
                    android:textColor="@color/appMainColor"
                    android:textSize="14sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </android.support.constraint.ConstraintLayout>

            <android.support.constraint.ConstraintLayout
                android:id="@+id/constraintLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.4"
                app:layout_constraintStart_toStartOf="parent">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:orientation="horizontal"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <TextView
                        android:id="@+id/pinView1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.25"
                        android:gravity="center"
                        android:text="-"
                        android:textColor="@color/appTintColor"
                        android:textSize="36sp" />

                    <TextView
                        android:id="@+id/pinView2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.25"
                        android:gravity="center"
                        android:text="-"
                        android:textColor="@color/appTintColor"
                        android:textSize="36sp" />

                    <TextView
                        android:id="@+id/pinView3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.25"
                        android:gravity="center"
                        android:text="-"
                        android:textColor="@color/appTintColor"
                        android:textSize="36sp" />

                    <TextView
                        android:id="@+id/pinView4"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.25"
                        android:gravity="center"
                        android:text="-"
                        android:textColor="@color/appTintColor"
                        android:textSize="36sp" />
                </LinearLayout>
            </android.support.constraint.ConstraintLayout>

        </android.support.constraint.ConstraintLayout>

As you can see the confirm button is centered with constraints in his parent.

But when I am running application in my Xperia z5 compact, a layout inspector shows that button is not centered.

Why does it happen and how to fix that?

Note if I am removing numberTextView with it's parent constarintLayout, the problem disappears. Note In my code I am programmatically setting a text to numberTextView. In case when I am commenting that code i.e not setting text programmatically, button draws correctly.


回答1:


app:layout_constraintHorizontal_bias="0.5"

layout_constraintHorizontal_bias this will make a small variation from center. try code given below,

            <Button
                android:id="@+id/confirmButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/rounded_corner_shape_tint"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:text="Confirm Pin"
                android:textColor="@color/appMainColor"
                android:textSize="14sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />



回答2:


Remove the below line from your button layout in xml.

app:layout_constraintHorizontal_bias="0.5"

Horizontal Bias: This allows us to position a view along the horizontal axis using a bias value, this will be relative to it’s constrained position.

For more information about Constraint Layout refer this article




回答3:


android:id="@+id/pinLayout" has hight of 250dp that's why button doesn't position at vertically center. try the code given below.

<android.support.constraint.ConstraintLayout 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">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/pinLayout"
        android:layout_width="match_parent"
        android:layout_height="250dp">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/cl_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:visibility="visible">

            <TextView
                android:id="@+id/numberTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:gravity="top"
                android:text="00000000"
                android:textColor="@color/appTintColor"
                android:textSize="18sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@id/cl_root"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.4"
            app:layout_constraintStart_toStartOf="parent">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:id="@+id/pinView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center"
                    android:text="-"
                    android:textColor="@color/appTintColor"
                    android:textSize="36sp" />

                <TextView
                    android:id="@+id/pinView2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center"
                    android:text="-"
                    android:textColor="@color/appTintColor"
                    android:textSize="36sp" />

                <TextView
                    android:id="@+id/pinView3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center"
                    android:text="-"
                    android:textColor="@color/appTintColor"
                    android:textSize="36sp" />

                <TextView
                    android:id="@+id/pinView4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:gravity="center"
                    android:text="-"
                    android:textColor="@color/appTintColor"
                    android:textSize="36sp" />
            </LinearLayout>
        </android.support.constraint.ConstraintLayout>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.4"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/confirmButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_corner_shape_tint"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="Confirm Pin"
            android:textColor="@color/appMainColor"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>



回答4:


try to use RelativeLayout instead of ConstraintLayout by changing all ConstraintLayout to RelativeLayout in your xml

<android.support.constraint.ConstraintLayout >
...
Your content
...
</android.support.constraint.ConstraintLayout>

to

<RelativeLayout >
...
Your content
...
</RelativeLayout>

then you might give set your button to be center in parent

android:layout_centerInParent="true"

for your TextView pinView1, pinView2, pinView3 and pinView4 you have simply put it inside a LinearLayout.



来源:https://stackoverflow.com/questions/56931441/android-studio-doesnt-draw-button-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!