向 Horizo​​ntalscrollView 添加布局

发布于 2024-11-17 12:37:26 字数 2715 浏览 5 评论 0原文

我正在尝试创建一个水平滚动视图,它应该包含一些可水平滚动的视图。

初始视图是一个水平滚动视图,内部有线性布局。

我有另一个视图,比如显示城市天气的屏幕。

如果我的数据库中有 5 个城市,我想将 5 个视图实例添加到水平滚动视图中。每个实例都有自己的事件,例如点击和http请求。

我可以从 xml 创建一个水平滚动视图,但我想动态地执行它。我一直在寻找一些例子来试图找到一个起点,但似乎什么也没有。

有人能指出我正确的方向吗?

这是我的horizo​​ntalscrollview 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);

因为滚动视图只能有一个孩子,我在里面添加了一个线性布局(不确定它是否应该是线性布局),并且我想将我的视图的所有实例添加到线性布局中。

PS:我已经为 iPhone 做了同样的应用程序,没有太多的痛苦。 Android 似乎让我太困惑了:(

I am trying to create a horizontalscrollview which is supposed to hold a few views that are scrollable horizontally.

The initial view is a horizontal scroll view with a linear layout inside it

I have another view, something like a screen that shows weather for a city.

If i have 5 cities in my database, i would like to add 5 instances of the view into the horizontalscroll view. And each instance has its own events such as clicks and http requests.

I can create a horizontalscrollview from the xml, but i would like to do it dynamically. I have been looking around for some examples to try to get a starting point, but nothing seems to be out there.

Can somebody point me to the right direction?

This is my 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>

This is how i tried to add a view (just to test if it works, though i am not sure even if it works, the view is just a layout or layout with functionality)

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);

Since a scrollview can only have one child, i added a linearlayout inside (not sure if it should be linear layout), and i want to add all the instances of my views into the linear layout.

P.S: i Have done the same application for iphone without too much pain. Android just seems to confuse me too much :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2024-11-24 12:37:26

好吧,这只是一个 xml 错误,android_id 错过了我的眼睛,线性布局不接受它,但很惊讶它没有抛出错误

Well,it just happens to be an xml error, the android_id missed my eye, the linear layout does not take it, but was surprised it did'nt throw an error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文