动态地向SlidingDrawer添加TextView

时间:2015-01-21 09:55:34

标签: android android-layout android-view android-sliding

我想动态地将TextView添加到SlidingDrawer。以下是我的XML代码:

<SlidingDrawer
    android:id="@+id/sdDrawerBottom"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:content="@+id/sdBottomContent"
    android:handle="@+id/sdBottomHandle"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/sdBottomHandle"
        android:layout_width="50dp"
        android:layout_height="20dp"
        android:background="@drawable/shape_rounded_corners_blue_top"
        android:scaleType="center"
        android:src="@drawable/icon_arrow_up_white" />

    <LinearLayout
        android:id="@+id/sdBottomContent"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="vertical"
        android:scrollbars="vertical" >

        <LinearLayout
            android:id="@+id/llBottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:scrollbars="horizontal" >
        </LinearLayout>
    </LinearLayout>
</SlidingDrawer>

这是我的java代码:

LinearLayout llBottomNavigation = (LinearLayout) mButtonsView
            .findViewById(R.id.llBottomNavigation);
for (int i = 0; i < navigationList.size(); i++) {
        TextView tvTab = new TextView(this);
        tvTab.setText(Content.getTitle(navigationList.get(i)));
        tvTab.setId(i);
                  tvTab.setTextColor(getResources().getColor(android.R.color.black));
        tvTab.setTextSize(12);
        if (Hardware.getScreenSize(this) > 4) {
            tvTab.setLayoutParams(new    LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
        } else {
            tvTab.setLayoutParams(new   LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
        }           tvTab.setSingleLine(true);
        tvTab.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
        llBottomNavigation.addView(tvTab);
        llBottomNavigation.invalidate();
    }

它没有向SlidingDrawer添加任何TextView。我的代码中有错误吗?

1 个答案:

答案 0 :(得分:0)

@ unflagged.destination,而addView你会通过视图传递layoutparams ...试试这个......

SlidingDrawer slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
LinearLayout llBottomNavigation = (LinearLayout) slidingDrawer.findViewById(R.id.llBottomNavigation);
for (int i = 0; i < 10; i++) {
    System.out.println("in loop, " + i);
    TextView tvTab = new TextView(this);
    tvTab.setText("item "+i);
    tvTab.setId(i);
    tvTab.setTextColor(getResources().getColor(android.R.color.black));
    tvTab.setTextSize(12);
    /*          if (Hardware.getScreenSize(this) > 4) {
        tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    } else {
        tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }           tvTab.setSingleLine(true);*/

    tvTab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });

    LinearLayout.LayoutParams paramsFortvTab = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    llBottomNavigation.addView(tvTab, paramsFortvTab);
    //llBottomNavigation.invalidate();
}

如果有任何疑问,请问......