Java Android-布局宽度包装内容,尽管设置为匹配父级?
我目前遇到的问题是,尽管在我的片段中所有布局都被设置为匹配父级,但除非某些内容明确占用屏幕宽度,例如带有大量文本字符串的TextView,否则布局最终会将其宽度包装到其内容中 这是XML: MainActivity.xml 在我将TextView文本设置为跨越屏幕宽度的很长字符串的情况下: 这正是我希望选项卡布局的样子,但我不希望必须定义TextView或类似的内容作为屏幕宽度,以使其以这种方式显示 我还尝试在onCreateView中设置片段的布局参数,但是,UI与第一幅图像中的相同 系列包装器片段OnCreateView 如果需要更多的代码片段或图像,请询问 如果有人能帮助我理解为什么会发生这种情况,我将不胜感激 谢谢。之所以发生这种情况,是因为match\u parent在ConstraintLayout中不起作用。相反,您需要从设计视图中使用match\u constraint,或者您可以通过设置heigh/width=0dp来设置match\u constraint 示例选项卡布局集android:layout\u width=0dp,它将在ConstraintLayout中作为匹配父项,如下所示Java Android-布局宽度包装内容,尽管设置为匹配父级?,java,android,android-layout,android-fragments,android-xml,Java,Android,Android Layout,Android Fragments,Android Xml,我目前遇到的问题是,尽管在我的片段中所有布局都被设置为匹配父级,但除非某些内容明确占用屏幕宽度,例如带有大量文本字符串的TextView,否则布局最终会将其宽度包装到其内容中 这是XML: MainActivity.xml 在我将TextView文本设置为跨越屏幕宽度的很长字符串的情况下: 这正是我希望选项卡布局的样子,但我不希望必须定义TextView或类似的内容作为屏幕宽度,以使其以这种方式显示 我还尝试在onCreateView中设置片段的布局参数,但是,UI与第一幅图像中的相同 系列包装
<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">
同样,这也适用于您的文本视图
<?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();
}
});
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;
}
<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">