Android - 布局宽度包装内容尽管设置为匹配父级?

时间:2018-03-23 02:32:17

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

我目前遇到的问题是尽管在我的片段中将所有布局设置为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或类似内容定义为屏幕宽度,以便以这种方式显示它。 / p>

我也尝试在onCreateView中设置片段的布局参数,但是,UI与第一张图片中的相同

系列包装碎片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;
}

如果需要更多代码段或图片,请询问。

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

谢谢。

1 个答案:

答案 0 :(得分:0)

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

标签布局集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

相同