我做了一个水平视图,里面有一个linearlayout。 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"
android:background="@drawable/bg"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/img_view"
/>
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:scrollbars="horizontal">
</HorizontalScrollView>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
我想创建20个按钮,它们应该在horizontalScroll视图中水平滚动,但我的活动只显示4个按钮并且不滚动。
我的代码是
public class NumericPage extends Activity{
private LinearLayout linear_layout;
private HorizontalScrollView hzv;
Button[] btn = new Button[10];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.numericpage);
hzv = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
linear_layout = (LinearLayout) findViewById(R.id.linearLayout1);
for (int j = 0; j < 10; j++)
{
btn[j] = new Button(this);
btn[j].setText("" + j );
btn[j].setId(j);
btn[j].setWidth(100);
btn[j].setHeight(40);
linear_layout.addView(btn[j]);
}
}
}
在android开始.. plz帮助!! 日Thnx
答案 0 :(得分:0)
您的LinearLayout是在HorizontalScrollView之外创建的,请更正您的布局
<?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"
android:background="@drawable/bg"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/img_view"
/>
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:scrollbars="horizontal">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
</LinearLayout>