【问题标题】:Android horizontalscrollview. Adding views pragmaticallyAndroid 水平滚动视图。务实地添加视图
【发布时间】:2012-07-17 09:32:48
【问题描述】:

我正在从 webservice 加载一些新闻站点,我想将这些添加到 HorizontalScrollView

在 iOS 上,我将通过循环我的新闻项目然后将文本标签作为子视图添加到 ScrollView 来实现。我正在尝试在 android 中做类似的事情,但我似乎无法让它正常工作。无论我做什么,项目都列在彼此的下方。

这是我添加视图的代码:

public void setupViews(ArrayList<News> news)
{
    HorizontalScrollView scrollView = (HorizontalScrollView) getView().findViewById(R.id.horizontalScrollView);
    LinearLayout layout = (LinearLayout) getView().findViewById(R.id.linearLayout);

    for(int i = 0; i < news.size(); i ++)
    {
        News newsItem = news.get(i);
        TextView head = new TextView(this.getActivity());
        head.setText(newsItem.getHead());
        head.setId(100+i);
        layout.addView(head);
    }
}

XML 如下所示:

<HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:id="@+id/horizontalScrollView" android:layout_gravity="center"
        >
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:id="@+id/linearLayout">
    </LinearLayout>
</HorizontalScrollView>

值得注意的是,我在片段中执行此操作..

【问题讨论】:

    标签: android horizontalscrollview


    【解决方案1】:

    您只能将单个子布局添加到 ScrollView

    在你的情况下:

    • 添加一个horizontal LinearLayout(通过将其添加到您的LinearLayout android:orientation="horizontal" 将方向设置为水平)
    • horizontal LinearLayout 中添加内容

    事情会在水平方向上就位:-)

    【讨论】:

    • 这正是他正在做的:)
    • 他只需要设置orientation="horizo​​ntal"
    • 我需要 android:orientation="horizo​​ntal" 才能工作。傻我,谢谢!
    • 我做到了 - 问题是因为新问题计时器的事情所以不会让我..
    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    相关资源
    最近更新 更多