使用并排浮动的元素或表格单元格创建图库

时间:2013-09-20 18:25:46

标签: java android android-layout

朋友我是Android的新手,我正面临所有初学者的问题,例如学习过程的第一步中的信息过载。

我想开发一个活动,其内容类似于图片库,每行2张图片。使用HTML + CSS,我将使用float:left和父元素宽度的一半创建div。或者也许创建一个每行有两个单元格的表。不会在HTML中为照片库做到这一点,但我知道它有效。

我想以编程方式执行此操作,并且已经看过使用TableLayout和RelativeLayout的解决方案。但我不明白如何在HTML中为数据(将是标题和图像)创建一个容器,如DIV,我想放在布局中。而且我不明白如何将它们中的每一个并排。

你会如何在布局和编程方面做到这一点?

我会用图片更新这个帖子,以便以后更好地理解我的问题。

1 个答案:

答案 0 :(得分:1)

使用以下XML代码作为模板在您的活动中充气,然后添加到ListView

  1. LayoutInflater的主要活动膨胀视图中:

    LayoutInflater inflater = getActivity() .getLayoutInflater();
    View view = inflater.inflate(R.layout.my_stats_layout, null); 
    
  2. 将上方视图用作ListView中的项目 - 将本教程用于自定义ListView   http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

  3. 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:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" >
    
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:src="@drawable/img1" />
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:src="@drawable/img2" />
            </RelativeLayout>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Img1" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Img2"
                android:textColor="#000" />
        </LinearLayout>
      </LinearLayout>
    </RelativeLayout>