android-fragments Android -布局宽度 Package 内容,尽管设置为匹配父?

ykejflvf  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(155)

我目前遇到的问题是,尽管我的片段中的所有布局都被设置为match_parent,但除非有什么东西显式地占用了屏幕宽度,比如带有大量文本字符串的TextView,否则布局最终会将其宽度 Package 到其内容中。
这是XML:

主要活动.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" />

系列 Package .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中设置片段的布局参数,但是UI与第一个图像中的UI相同

CreateView上的序列 Package 函数片段

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;
}

如果需要更多的代码片段或图像,请询问。
如果有人能帮助我理解为什么会发生这种情况,我将不胜感激。

  • 谢谢-谢谢
vohkndzv

vohkndzv1#

这是因为match_parentConstraintLayout中不起作用。您需要使用设计视图中的match_constraint,或者您可以通过设置heigh/width = 0 dp来设置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

pcww981p

pcww981p2#

将这行代码添加到Recyclerview Adapter中的onCreateViewHolder()方法中,它对我起作用。

//  Add this line if you are using data binding.

binding.root.layoutParams = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
return ViewHolder(binding) // change the class of ViewHolder with your ViewHolder

//  Add this line if you are not using data binding.

val view  = inflater.inflate(R.layout.movie_adapter_ly,parent,false)
view.rootView.layoutParams = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
// change the class of ViewHolder with your ViewHolder class
return ViewHolder(view)

相关问题