【问题标题】:Android - Layout Width wrapping content despite being set to match parent?Android - 尽管设置为匹配父级,但布局宽度包装内容?
【发布时间】:2018-03-23 02:32:16
【问题描述】:

我目前遇到的问题是,尽管在我的片段中将所有布局设置为 match_parent,除非某些内容明确占用了屏幕宽度,例如带有大量文本字符串的 TextView,布局结束将它们的宽度包裹到它们的内容中。

这是 XML:

MainActivity.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context="es.esports_app.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:minHeight="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/notQuiteBlack"
        app:layout_constraintTop_toTopOf="parent">

    </android.support.v7.widget.Toolbar>

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toTopOf="@+id/navigationViewLayoutWrapper"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mainToolbar">

        <!-- Where fragment content is inserted -->
        <FrameLayout
            android:id="@+id/Container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </FrameLayout>

    </android.support.constraint.ConstraintLayout>

    <include
        android:id="@+id/navigationViewLayoutWrapper"
        layout="@layout/main_bottom_navigation_bar" />

</android.support.constraint.ConstraintLayout>

<ListView
    android:id="@+id/navigation_drawer_list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111" />

series_wrapper.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<android.support.design.widget.TabLayout
    android:id="@+id/seriesSectionTabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/notQuiteBlack"
    app:tabTextColor="@color/mdGrey400"
    app:tabIndicatorColor="@color/white"
    app:tabSelectedTextColor="@color/white"
    app:tabMode="scrollable"
    app:tabGravity="fill"
    app:tabMaxWidth="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent">


</android.support.design.widget.TabLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="YAY"
    app:layout_constraintTop_toBottomOf="@id/seriesSectionTabs"/>

</android.support.constraint.ConstraintLayout>

片段的添加位置

holder.eventSeriesWrapper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AppCompatActivity appCompatActivity = (AppCompatActivity) view.getContext();
            Fragment fragment = new SeriesWrapperFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("seriesID", eventSeries.id);
            bundle.putInt("seriesLength", eventSeries.seriesLength);
            fragment.setArguments(bundle);
            appCompatActivity.getFragmentManager().beginTransaction().replace(R.id.Container, fragment).addToBackStack(null).commit();
        }
    });

The above code results in this

在我将 TextView 文本设置为跨越屏幕宽度的非常长的字符串的情况下:

This is the result

这正是我希望选项卡布局看起来的样子,但我不想必须将 TextView 或类似的东西定义为屏幕宽度才能让它以这种方式显示。

我也尝试在 onCreateView 中设置 Fragment 的 Layout Params,但是 UI 与第一张图片中的相同

Series Wrapper Fragment OnCreateView

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.series_wrapper, container, false);
    view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    if(this.getArguments() != null){
        this.seriesID = this.getArguments().getInt("seriesID");
        this.seriesLength = this.getArguments().getInt("seriesLength");
    }
    else{
        this.seriesID = 0;
        this.seriesLength = 0;
    }

    return view;
}

如果需要更多代码 sn-ps 或图像,请询问。

如果有人可以帮助我了解为什么会发生这种情况,将不胜感激。

谢谢。

【问题讨论】:

    标签: java android android-layout android-fragments android-xml


    【解决方案1】:

    发生这种情况是因为match_parentConstraintLayout 中不起作用。相反,您需要在设计视图中使用match_constraint,或者您可以通过设置高度/宽度= 0dp 来设置match_constraint

    例如,您的选项卡布局集 android:layout_width="0dp" 将在 ConstraintLayout 中表现为 match_parent,就像这样

    <android.support.design.widget.TabLayout
        android:id="@+id/seriesSectionTabs"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/notQuiteBlack"
        app:tabTextColor="@color/mdGrey400"
        app:tabIndicatorColor="@color/white"
        app:tabSelectedTextColor="@color/white"
        app:tabMode="scrollable"
        app:tabGravity="fill"
        app:tabMaxWidth="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
    

    TextView 也是如此

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多