线性布局宽度和高度不能设置为与父级匹配

时间:2017-09-28 15:35:52

标签: android xml

我正在开发一个med提醒应用程序 我有两个文本字段的线性布局,无法设置我的布局宽度和高度以匹配父调整到我的代码

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

5 个答案:

答案 0 :(得分:3)

您的问题是由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>

答案 1 :(得分:0)

  • 将最外层布局设为LinearLayout而不是ConstraintLayout
  • 删除内部主LinearLayout(您不需要它,因为外部线性布局可以做您想要的)
  • 权重LinearLayoutTextView的文字视图height0widthmatch_parent,因此可以根据屏幕进行调整< / LI>

答案 2 :(得分:0)

要充分利用layout_weight,请将height设置为0dp。此外,将两个文本视图都设置为高度match_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"/>

答案 3 :(得分:0)

似乎你在约束布局和带有权重属性的线性布局之间混乱。 同时我订了一些订单,请查看是否符合您的要求。我不 - 请更好地解释你的需求:

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

enter image description here

答案 4 :(得分:0)

试试这个,希望它能起作用..

<td style="font-weight: bold;" class="auto-style10" headers="Sample Forms">
 &nbsp;Sample Forms&nbsp;<br /><a 
href="../../Skin/faq_help/help%20Library/PSM%20templates/Oilandnh3Log.xlxs" 
target="_blank">Ammonia Loss and Oil Log</a> <br />
<a href="../../Skin/faq_help/help%20Library/PSM%20templates/Leak 
Calculator.xls" target="_blank">Ammonia Loss Calculator<br />
<a href="../../Skin/faq_help/help%20Library/PSM%20templates/CONTRACTORS On-
Site Safety Review Form.docx" target="_blank">Contractor On-Site Safety 
Review<br />