gpt4 book ai didi

java - fragment 内的布局未渲染

转载 作者:行者123 更新时间:2023-12-01 12:54:26 28 4
gpt4 key购买 nike

我有一个Fragment Layout,它加载得很好,但是布局的元素是通过编程添加的,因为我不知道这个布局需要多少个选项卡。

我的问题是我可以获得线性布局的子项计数,它们都应该被添加,但没有显示任何内容:(

这是我尝试过的:

  • 尝试使用 TableRow、TableLayout、相对布局、在内部创建另一个线性布局等。其中一些选项将仅显示 1 个子项,而不是应显示的 16 个子项
  • 尝试在 XML 中并通过布局参数手动设置按钮的宽度。该按钮已显示,但仅显示一个按钮。

这是我的 onCreateView 方法的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.descriptive_tables_tabs, null, true);
tabsLayout = (LinearLayout) view.findViewById(R.id.myTabsRow);
args = this.getArguments();
toLoadTabs = (FrameLayout) view.findViewById(R.id.toLoadTable);
tabButtons = new ArrayList<Button>();
tabTitles = args.getStringArrayList("tabTitles");
Log.i(LOG_TAG, "Tabs Title Size: " + tabTitles.size());
float weight = 1.0f / tabTitles.size();
Log.i(LOG_TAG, "WEIGHT: " + weight);
for (String s : tabTitles) {
Button mButton = (Button) inflater.inflate(R.layout.tab_button, null, true);
View separator = inflater.inflate(R.layout.separator, null, true);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, weight);
mButton.setLayoutParams(params);
mButton.setText(s);
tabButtons.add(mButton);
Log.i(LOG_TAG, "Tabs: " + s);
tabsLayout.addView(mButton);
tabsLayout.addView(separator);
}
Log.i(LOG_TAG, "Layout Childs: " + tabsLayout.getChildCount());
return view;
}

这是 LOGCAT 信息,显示数据正在传递并且子项存在。

06-02 17:47:37.314  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs Title Size: 8
06-02 17:47:37.314 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ WEIGHT: 0.125
06-02 17:47:37.319 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: FlyingHours
06-02 17:47:37.319 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: QuantityPerAssembly
06-02 17:47:37.319 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: UseFactor
06-02 17:47:37.319 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: Failures
06-02 17:47:37.319 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: ManHours
06-02 17:47:37.324 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: Aborts
06-02 17:47:37.324 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: MeanTimeBetweenFailures
06-02 17:47:37.324 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: RepairAndMaintenanceCritical
06-02 17:47:37.324 14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Layout Childs: 16

这是问题的屏幕截图,灰色区域是我的选项卡应该所在的位置:( Here is a screenshot of the problem

谢谢!

编辑 - 添加所使用的 XML 布局:

descriptive_tables_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">

<LinearLayout
android:id="@+id/myTabsRow"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="7"
android:background="@color/light_bg"
android:orientation="horizontal"
android:weightSum="1">

</LinearLayout>

<ScrollView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="93"
android:background="@color/light_bg">

<FrameLayout
android:id="@+id/loadTabFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_bg"
android:orientation="horizontal"></FrameLayout>
</ScrollView>
</LinearLayout>

这是 tab_button.xml

<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="@drawable/tab_button_selector"
android:gravity="center_vertical|center_horizontal"
android:onClick="changeTab"
android:paddingLeft="15dip"
android:layout_weight="0.1"
android:text="@string/model_information"
android:textColor="@color/dark_blue_bg"
android:textSize="@dimen/tab_title" />

编辑:布局本身根本不渲染。选项卡行的背景应为白色并与 fragment 的其余部分匹配。

最佳答案

所以我终于想通了......出于某种原因,布局只接受一个子项(或者只渲染它,因为子项计数为 16......)所以我所做的是使用 TableLayout 作为 R.id.myTabsRow;

在我的 fragment onCreateView 中,我创建了一个 TableRow 并将所有子项添加到 TableRow 中,在循环之后我将行添加到TableLayout 现在一切都渲染得很好!

这是我的最终工作代码,以防对某人有帮助:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
args = this.getArguments();
tabTitles = args.getStringArrayList("tabTitles");
view = inflater.inflate(R.layout.descriptive_tables_tabs, null);
tabsLayout = (TableLayout) view.findViewById(R.id.myTabsRow);

toLoadTabs = (FrameLayout) view.findViewById(R.id.toLoadTable);
tabButtons = new ArrayList<Button>();
//ArrayList<DescriptiveTablesFragment> tables = (DescriptiveTablesFragment) args.getParcelableArrayList("tables");

Log.i(LOG_TAG, "Tabs Title Size: " + tabTitles.size());
float weight = 1f;
Log.i(LOG_TAG, "WEIGHT: " + weight);

TableRow myRow = new TableRow(getActivity());
myRow.setWeightSum((float) tabTitles.size());
TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, weight);
for (String s : tabTitles) {
Button mButton = (Button) inflater.inflate(R.layout.tab_button, null);
View separator = inflater.inflate(R.layout.separator, null);
mButton.setText(s);
mButton.setLayoutParams(params);
tabButtons.add(mButton);
myRow.addView(mButton);
myRow.addView(separator);
}
tabsLayout.addView(myRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
return view;
}

关于java - fragment 内的布局未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24004574/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com