【问题标题】:LinearLayout doesn't fill the parentLinearLayout 不填充父级
【发布时间】:2015-11-25 20:52:59
【问题描述】:

LinearLayout 不填充父级。我需要按宽度填充所有元素。

这是智能手机上显示的照片。但在 Android Studio xml 预览中一切正常。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/bg_color"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/editText3"
        android:hint="Сообщение" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:hint="Тема" />

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="дата"
            android:id="@+id/textView"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="время"
            android:id="@+id/textView2"
            android:layout_weight="1" />
    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Важность"
        android:id="@+id/textView3" />

    <SeekBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

【问题讨论】:

  • 这是 Activity 的布局还是包含在另一个片段/容器中?顺便说一句,自 API 8 起,fill_parent 已被弃用。请改用match_parent
  • 这是一个片段布局。其他具有线性布局和 fill_parent 属性的片段运行良好。
  • 片段的 onCreateView 是这样的吗? return inflater.inflate(R.layout.my_layout, container, false);
  • @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.reminder_add, container, false);返回视图; }
  • 暂时都是SherlockFragment的代码。

标签: android android-linearlayout fill-parent


【解决方案1】:

到处都用 match_parent 替换 fill_parent。除此之外,您必须在 Activity 的 XML 中为片段的容器布局设置 match_parent。

【讨论】:

  • fill_parent 没有问题。我为框架布局编写了 wrap_content,而 fill_parrent 解决了我的问题。谢谢你的回答)
  • 是这样想的 :) 你仍然应该使用 match_parent 而不是 fill_parent。根据What is the difference between match_parent and fill_parent?,除了命名之外,它们之间实际上没有区别,但不推荐使用 fill_parent ——可能只是为了让人们使用“不那么容易混淆”的名称“匹配父级”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
相关资源
最近更新 更多