我正在尝试创建一个horizontalscrollview,该应该包含可水平滚动的几个视图。向HorizontalscrollView添加布局
初始视图是与它里面
线性布局的水平滚动视图我有另一种观点,如屏幕,显示天气一个城市的东西。
如果我在我的数据库中有5个城市,我想将视图的5个实例添加到horizontalscroll视图中。而且每个实例都有自己的事件,如点击和http请求。
我可以从xml创建一个horizontalscrollview,但我想动态地做到这一点。我一直在寻找一些例子来试图获得一个出发点,但似乎没有任何东西出现。
有人可以指向正确的方向吗?
这是我horizontalscrollview 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">
<RelativeLayout android:layout_height="50dp"
android:id="@+id/TableRow01" android:layout_width="fill_parent"
android:background="#82db7a">
<ImageView android:id="@+id/imageView1" android:src="@drawable/logo_topbar"
android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageButton android:background="@drawable/topicon_btn"
android:src="@drawable/btn_icon_locateme" android:layout_gravity="center_vertical"
android:layout_height="45dp" android:id="@+id/locatemebtn"
android:layout_width="wrap_content" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:layout_margin="5dp" />
<ImageButton android:background="@drawable/topicon_btn"
android:src="@drawable/btn_icon_star" android:layout_height="45dp"
android:id="@+id/favbtn" android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/locatemebtn" android:layout_alignTop="@+id/locatemebtn"
android:layout_alignBottom="@+id/locatemebtn" />
</RelativeLayout>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:fillViewport="true"
android:id="@+id/scrollView1" android:layout_height="fill_parent"
android:layout_width="fill_parent" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android_id="@+id/mainScroll">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
这是我尝试添加视图(,只是为了测试它是否正常工作,虽然我不知道,即使它的工作原理,认为仅仅是一个布局或与功能布局)
LinearLayout mainscroll=(LinearLayout) findViewById(R.id.mainScroll);
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.weather_tab, null);
mainscroll.addView(view);
由于滚动视图只能有一个孩子,我添加了一个的LinearLayout内(不知道是否应该是线性的布局),我想我的看法所有的实例添加到线性布局。
P.S:我做了同样的应用程序,没有太多的痛苦。 Android似乎让我困惑太多:(