【问题标题】:Why does adding a View to my layout change the width of the entire layout?为什么向我的布局添加视图会改变整个布局的宽度?
【发布时间】:2017-10-11 21:26:17
【问题描述】:

我在布局底部添加了一个视图,以便在该元素的末尾添加一个行分隔符。问题是,将此视图放入布局后会填满屏幕的整个宽度,但是当我取出视图时,它显示为正确的大小(在我的测试中约为 400dp)。为什么添加的 View 会导致布局填满整个屏幕。

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:orientation="vertical"
    android:paddingLeft="6dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:textColor="@android:color/darker_gray"
            android:textSize="12sp"
            tools:text="Hello, Label"
            android:paddingBottom="6dp"
            android:paddingLeft="6dp"/>

        <TextView
            android:id="@+id/text_error"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:textColor="@color/sr_red"
            android:textSize="12sp"
            tools:text="Error!"/>
    </LinearLayout>


<!-- this View causes the parent layout to fill the whole screen width--> 
     <View
         android:layout_width="wrap_content"
         android:layout_height="1dp"
         android:layout_marginTop="2dp"
         android:background="@android:color/darker_gray"
         />


</LinearLayout>

这是导致问题的最底层。

我正在这样修改视图:

val inputLayout = createLabelLayout(context, component.name, button)
viewGroup.addView(inputLayout.root)
inputLayout.setWidth(width)

这里是相关的类

private fun <T : View> createLabelLayout(context: Context, text: String, child: T): LabelLayout<T> {
    val layout = LabelLayout(context, child)
    layout.label.text = text
    return layout
}



private class LabelLayout<out T : View>(context: Context, child: T) {
    val root = LayoutInflater.from(context).inflate(R.layout.item_custom_field_label, null, false) as ViewGroup
    val label = root.findViewById(R.id.text_label) as TextView
    val error = root.findViewById(R.id.text_error) as TextView


    init {
        root.addView(child, root.childCount - 1)

    }

    fun setWidth(width: Int) {
        val params = root.layoutParams
        params.width = width
        root.layoutParams = params
    }
}

【问题讨论】:

  • 您的根LinearLayoutwidthheight 设置为match_parent。至少它是height 应该是wrap_content
  • 对,但是当我调用 setWidth() 时,我正在重置根元素的宽度。问题是,如果我注释掉 xml 中最底部的视图(作为分隔线的视图),那么根元素会格式化为正确的宽度。所以它似乎是导致问题的布局中的最后一个视图。

标签: android xml android-layout kotlin


【解决方案1】:

好的,所以我通过将根 LinearLayout 和子 LinearLayout 都更改为 wrap_content 来解决此问题。在我只更改父布局宽度之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-16
    • 2018-09-23
    • 2020-08-27
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多