Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 线性布局的宽度和高度不能设置为与父级匹配_Android_Xml - Fatal编程技术网

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
    ,以便它们可以根据屏幕进行调整

要充分利用布局重量,请将高度设置为0dp。另外,让两个文本视图的高度都与父视图匹配会把事情搞砸

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