Android studio没有';不要正确地画按钮
在我的Android studio项目中,我有一个内部带有约束布局的片段。这里是XMLAndroid studio没有';不要正确地画按钮,android,button,constraints,draw,shift,Android,Button,Constraints,Draw,Shift,在我的Android studio项目中,我有一个内部带有约束布局的片段。这里是XML <android.support.constraint.ConstraintLayout android:id="@+id/pinLayout" android:layout_width="match_parent" android:layout_height="250dp" app:layo
<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>
但当我在XperiaZ5Compact中运行应用程序时,一个布局检查器显示按钮并没有居中
为什么会发生这种情况以及如何解决
注意如果我正在删除NumberExtView及其父constarintLayout,问题就会消失。
注意在我的代码中,我以编程方式将文本设置为numberTextView。如果我对代码进行注释,即没有以编程方式设置文本,则按钮将正确绘制。从xml中的按钮布局中删除下面的行
app:layout_constraintHorizontal_bias="0.5"
水平偏差:
这允许我们使用偏移值沿水平轴定位视图,这将相对于其受约束的位置
有关约束布局的详细信息,请参阅
layout\u约束水平偏差
。尝试下面给出的代码
<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" />
android:id=“@+id/pinLayout”
的高度为250dp
,这就是为什么按钮不位于垂直中心的原因。请尝试下面给出的代码
<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>
尝试使用RelativeLayout而不是ConstraintLayout,方法是将xml中的所有ConstraintLayout更改为RelativeLayout
<android.support.constraint.ConstraintLayout >
...
Your content
...
</android.support.constraint.ConstraintLayout>
对于您的TextView pinView1、pinView2、pinView3和pinView4,您只需将其放在线性布局中即可 这对我没有帮助。按钮未垂直居中。水平偏差约束如何影响垂直位置?同时从按钮的父约束布局中删除“app:layout\u constraintHeight\u percent=“0.4”。此外,不需要将约束布局作为每个视图的父视图。请参阅上面的文章以了解有关ConstraintLayout的更多信息。这对我没有帮助。按钮未垂直居中。水平偏差约束如何影响垂直位置?问题是,您声明的按钮位置不正确。将按钮声明在第二个constraintLayout2外部因为将constraintLayout2的高度设置为与父视图匹配,它覆盖了整个父视图。
<android.support.constraint.ConstraintLayout >
...
Your content
...
</android.support.constraint.ConstraintLayout>
<RelativeLayout >
...
Your content
...
</RelativeLayout>
android:layout_centerInParent="true"