在一个活动中动态分配两个布局的问题
我正在尝试处理需要在同一活动布局中的两个水平滚动视图中分配两个 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>