Android 线性布局的宽度和高度不能设置为与父级匹配
我正在开发一个医疗提醒应用程序 我有两个文本字段的线性布局,无法设置布局宽度和高度,以匹配根据我的代码调整的父布局Android 线性布局的宽度和高度不能设置为与父级匹配,android,xml,Android,Xml,我正在开发一个医疗提醒应用程序 我有两个文本字段的线性布局,无法设置布局宽度和高度,以匹配根据我的代码调整的父布局 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.andr
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nsk.medapp.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Add Pills"
android:textSize="50sp"
android:textAlignment="center"
android:layout_weight="1"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Edit Pills"
android:textSize="50sp"
android:textAlignment="center"
android:layout_weight="1"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
- 将最外层布局设置为
而不是LinearLayout
ConstraintLayout
- 删除内部主
(您不需要它,因为外部线性布局可以实现您想要的功能)线性布局
- 将权重
和LinearLayout
与文本视图的TextView
设置为height
和0
设置为width
,以便它们可以根据屏幕进行调整match\u parent
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Add Pills"
android:textSize="50sp"
android:textAlignment="center"
android:layout_weight="1"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Edit Pills"
android:textSize="50sp"
android:textAlignment="center"
android:layout_weight="1"/>
您的问题是由ConstraintLayout引起的。如果您不需要它,只需删除并让LinearLayout作为父级
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Add Pills"
android:textSize="50sp"
android:textAlignment="center"
android:layout_weight="1"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Edit Pills"
android:textSize="50sp"
android:textAlignment="center"
android:layout_weight="1"/>
</LinearLayout>
似乎约束布局和带有权重属性的线性布局之间存在混乱。 同时我下了一些订单,请看看是否是您需要的。我不是-请更好地解释您需要:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:gravity="center"
android:text="Add Pills"
android:textAlignment="center"
android:textSize="50sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:text="Edit Pills"
android:textAlignment="center"
android:textSize="50sp" />
</LinearLayout>
试试这个,希望它能奏效。。
你所说的“适应我的代码”是什么意思?你不需要约束布局来实现这一点,只需将你的线性布局设置为你的根,你就可以发布你想要实现的内容的快照。查看你当前的样式/主题,查看隐藏在不同资源文件中的默认边距/填充。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Add Pills"
android:textSize="50sp"
android:textAlignment="center"
android:layout_weight="1"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Edit Pills"
android:textSize="50sp"
android:textAlignment="center"
android:layout_weight="1"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>