里面的片段布局不渲染 [英] Layouts inside fragment not rendering
问题描述
我有一个片段布局
它加载罚款,但都通过编程添加布局的元素,因为我不知道我有多少标签需要此布局。
I have an Fragment Layout
which loads fine but the elements of the layout are been added programatically because I don't know how many tabs I need for this layout.
我的问题是,我可以得到线性布局的孩子数,他们都应该加入,但不显示任何内容:(
My problem is that I can get the child count of the Linear Layout and they are all supposed to be added but nothing is displayed :(
下面是我的尝试:
- 尝试使用的TableRow,TableLayout,相对布局,内部创建另一个线性布局等。其中一些选项将显示只有一个孩子,而不是那些应该显示16
- 尝试设置在XML手动和通过布局PARAMS按钮的宽度。该按钮将显示,但只有一个按钮。
下面是code我onCreateView方式:
Here is the code for my onCreateView method:
@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的信息,显示该数据被传递和儿童的存在..
Here is the LOGCAT info that shows that the data is being passed and the childs exist..
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, the gray area is where my tabs are supposed to go :(
谢谢!
编辑 - 添加正在被使用的XML布局:
EDIT - Adding the XML layouts that are been used:
descriptive_tables_tab.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
And here is the 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" />
编辑:本身没有呈现在所有的布局。选项卡行的背景应该是白色的搭配片段的其余部分。
The layout itself is not rendering at all. The background of the tabs row should be white and match the rest of the fragment.
推荐答案
所以,我终于想通了....出于某种原因,布局只接受一个孩子(或只是渲染它,因为孩子数是16。 ...)
所以我所做的就是使用 TableLayout
的的 R.id.myTabsRow;
So I finally figured it out.... For some reason the Layout was only accepting one child (or was only rendering it because the child count was 16....)
SO what I did was use a TableLayout
for the the R.id.myTabsRow;
和我的片段里面 onCreateView
我创建了一个的TableRow
,并添加了所有孩子的在的TableRow
和循环之后我加入了行到 TableLayout
,现在一切都呈现精致!
And inside my Fragment onCreateView
I created a TableRow
and added all the childs to the TableRow
and after the loop I added the row to the TableLayout
and now everything is rendering fine!!
下面是我最后的工作在$情况c $ C是有帮助的人:
Here is my final Working code in case it is helpful to someone:
@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;
}
这篇关于里面的片段布局不渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!