如何在Android中创建这样的视图?

时间:2013-09-11 10:29:55

标签: java android android-imageview horizontalscrollview

我想创建一个如下图所示的布局(注意红色圆圈)。

enter image description here

我在horizontalScroll View的帮助下尝试了这个并制作了一个像下面那样丑陋的人:

enter image description here

我希望这看起来像上面那个,我应该使用什么,以便我可以得到每个温度值之间的垂直条,

另一件事是,是否可以在水平滚动视图中添加多个视图,因为我只添加了动态创建的图像视图,请参阅我的图。

这是我到目前为止尝试过的代码:

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/world_map"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/horizontalScrollView"
    android:layout_alignLeft="@+id/horizontalScrollView"
    android:text="Flags"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="80dp"
    android:padding="0dp"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/rootlinear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="0dp" >
    </LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

的java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dynamic_views);
            createViewsDynamically(context);

}

private void createViewsDynamically(final Context context) {
    // TODO Auto-generated method stub

    LinearLayout rootLayout = (LinearLayout) findViewById(R.id.rootlinear);
    for (int i = 0; i < flags.length; i++) {

        ImageView img = new ImageView(context);
        img.setBackgroundResource(flags[i]);
        img.setLayoutParams(new ViewGroup.LayoutParams(80,90));

        img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Test", duration).show();
            }
        });

        rootLayout.addView(img);

    }

}

1 个答案:

答案 0 :(得分:1)

您想要以水平形式显示项目列表。我可以说你的解决方案是水平滚动视图。访问此链接,演示水平列表视图:

http://www.androiddevelopersolution.com/2012/11/horizontal-listview-in-android-example.html