java - Android - 尽管布局宽度设置为匹配父级,但仍包裹内容?

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

我目前遇到的问题是,尽管在我的 fragment 中将所有布局都设置为 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>

添加 fragment 的位置

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 的布局参数,但是,UI 与第一张图片中的相同

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

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

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

谢谢。

最佳答案

发生这种情况是因为 match_parentConstraintLayout 中不起作用。相反,您需要使用设计 View 中的 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 也同样如此

关于java - Android - 尽管布局宽度设置为匹配父级,但仍包裹内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49441499/

相关文章:

java - 求解用 Java 语言编写的数学方程

java - 使用 `dayTimeDuration` 函数时编译 XSL 文件时出现问题

php - 从 MySQL 表获取排名到 TextView

java - 里面有 listview 的 Viewpager

android studio - zxing 条码扫描仪 - 自定义布局

java - 每个对象如何彼此不同

java - Websphere 中的 Eclipse Scout RAP UI 部署

java - firebase 如何检测到 "app_remove"

java - Android - findViewById() 和动态设置 ID

java - 通过单击按钮展开和折叠 Relativelayout