[英]Layouts inside fragment not rendering
我有一个Fragment Layout
,可以很好地加载,但是布局的元素是通过编程方式添加的,因为我不知道该布局需要多少选项卡。
我的问题是,我可以获取线性布局的子项计数,并且应该全部添加它们,但是什么也不显示:(
这是我尝试过的:
这是我的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
这是问题的屏幕截图,灰色区域是我的选项卡应该去的地方:(
谢谢!
编辑-添加已使用的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" />
编辑:布局本身根本不呈现。 选项卡行的背景应为白色,并与其余片段匹配。
因此,我终于弄清楚了...。由于某种原因,Layout只接受一个孩子(或仅因为孩子数为16而才渲染它。)所以我所做的就是为R.id.myTabsRow;
使用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;
}
没有R.layout.descriptive_tables_tabs
的xml很难说,但是问题一定出在R.id.myTabsRow
。
例如,如果将width设置为wrap_content
,它将是不可见的。 这是因为子视图的权重设置为0宽度。 这只会声明可用空间,在这种情况下将为0。
编辑:
从添加的xml来看,问题似乎确实在R.id.myTabsRow中:
android:layout_height="0dip"
将此设置为wrap_content应该可以解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.