在一个活动中动态分配两个Layout / TableRow

时间:2012-04-08 09:30:40

标签: android android-linearlayout tablerow

在一项活动中动态分配两个布局的问题

我正在尝试处理一个逻辑,我需要在同一个Activity Layout中的两个Horizo​​ntal Scroll视图中分配两个LinearLayout或TableRow,但它似乎无法正常工作。任何帮助将不胜感激

此 是 码 这里的活动:

public class ViewTestActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    LinearLayout LinearLayoutInScrollView;
    LinearLayout Inner1HorizontalLinearLayout;
    LinearLayout Inner2HorizontalLinearLayout;
    ScrollView ScrollViewMain;
    HorizontalScrollView horizontalScrollViewOfMain;
    TextView textViewOne;
    TextView textViewTwo;
    Button verticalScrollTriggerButton;
    Button horizontalScrollTriggerButton;

    @Override    

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            LinearLayoutInScrollView = (LinearLayout) findViewById(R.id.MainscrollViewLayout);

            horizontalScrollViewOfMain = new HorizontalScrollView(this);
            LinearLayoutInScrollView.addView(horizontalScrollViewOfMain);



            verticalScrollTriggerButton = new Button(this);
            verticalScrollTriggerButton.setText("CheckVerticalScroll");
            verticalScrollTriggerButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    for (int i = 0; i < 30; i++) {
                        textViewOne = new TextView(getApplicationContext());
                        textViewOne.setBackgroundColor(0xaa000000);
                        textViewOne.setText("This is Dynamic");
                        LinearLayoutInScrollView.addView(textViewOne);
                    }

                }
            });

            horizontalScrollTriggerButton = new Button(this);
            horizontalScrollTriggerButton.setText("CheckHorizontalScroll");
            horizontalScrollTriggerButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Inner1HorizontalLinearLayout = new LinearLayout(getApplicationContext());
                    Inner2HorizontalLinearLayout = new LinearLayout(getApplicationContext());               
                    for (int i = 0; i < 30; i++) {
                        textViewOne = new TextView(getApplicationContext());
                        textViewOne.setBackgroundColor(0xaa000000);
                        textViewOne.setText("This is Dynamic textViewOne");
                        Inner1HorizontalLinearLayout.addView(textViewOne);
                        textViewTwo = new TextView(getApplicationContext());
                        textViewTwo.setBackgroundColor(0xaa000000);
                        textViewTwo.setText("This is Dynamic textViewTwo");
                        Inner2HorizontalLinearLayout.addView(textViewTwo);                  
                    }
                    horizontalScrollViewOfMain.addView(Inner1HorizontalLinearLayout);
                    horizontalScrollViewOfMain.addView(Inner2HorizontalLinearLayout);
                }

            });

            LinearLayoutInScrollView.addView(verticalScrollTriggerButton);
            LinearLayoutInScrollView.addView(horizontalScrollTriggerButton);

        }

此 是 码 这里是main.xml:

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

    <ScrollView
        android:id="@+id/MainscrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/MainscrollViewLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >


        </LinearLayout>

    </ScrollView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我看到的第一个问题是这两行:

horizontalScrollViewOfMain.addView(Inner1HorizontalLinearLayout);
horizontalScrollViewOfMain.addView(Inner2HorizontalLinearLayout);

任何scrollView都是一个框架视图,只能有一个内部视图。

http://developer.android.com/reference/android/widget/HorizontalScrollView.html

我现在不在电脑上运行代码/查看更多细节,而且您没有详细说明您的具体问题。但看看这是否解决了你的问题。