片段内的布局不呈现

时间:2014-06-02 22:56:21

标签: java android android-fragments android-linearlayout

我有一个Fragment Layout加载正常,但是以编程方式添加了布局元素,因为我不知道这个布局需要多少个标签。

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

以下是我的尝试:

  • 尝试使用TableRow,TableLayout,相对布局,在里面创建另一个线性布局等。其中一些选项只显示一个孩子而不是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" />

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

2 个答案:

答案 0 :(得分:1)

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

在我的片段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;
}

答案 1 :(得分:0)

如果没有R.layout.descriptive_tables_tabs的xml,很难说,但问题必须是R.id.myTabsRow

例如,如果width设置为wrap_content,则它将不可见。这是因为子视图的权重设置为0宽度。这只会占用可用空间,在这种情况下为0。

修改

从添加的xml中,似乎问题确实存在于R.id.myTabsRow中:

android:layout_height="0dip"

将此设置为wrap_content应解决它。