Android布局有两列

时间:2012-09-18 08:15:00

标签: android android-layout

Android新手和整个线性/相对布局的东西。需要帮助确定如何制作这种布局:

enter image description here

到目前为止,我所做的是:

Linear Layout(Vertical)
---Textview ("fixpix.")
---Linear Layout (Horizontal) //first column
------Linear Layout (Vertical, weight 1) //even more nesting here
------Linear Layout (Vertical, weight 1) //even more nesting here
------Linear Layout (Vertical, weight 1) //even more nesting here
---Linear Layout (Horizontal) //last column
------Linear Layout (Vertical, weight 1) //even more nesting here
------Linear Layout (Vertical, weight 1) //even more nesting here
------Linear Layout (Vertical, weight 1) //even more nesting here
---Button ("Learn More.")

我收到很多关于不应该嵌套权重的警告等等。我甚至不确定这是否是正确的方法。有人可以建议更好的方式,而不是复杂的

6 个答案:

答案 0 :(得分:5)

构建即使是中等复杂布局的最佳提示。

使用普通视图作为占位符,而不是ImageViews,TextViews等。将每个元素的背景颜色(包括封闭的Linears / Relatives / Frames等)设置为不同的颜色。这样,你可以准确地看到每个元素正在做什么并调整你的权重,“相对性”(例如layoutToTheRightOf等)和大小,直到布局看起来很完美。然后用您想要的元素替换占位符。

如果您正在努力使用现有布局,请将XML复制/粘贴到文本编辑器中。执行上述操作,然后将各个元素逐个复制/粘贴到其占位符上。您可以在短时间内控制您的布局。

当然,很好地理解每个ViewGroup的工作原理对于构建优秀的Android应用程序至关重要。

祝你好运。

答案 1 :(得分:1)

尝试使用GridView解决此问题。使用它代替LinearLayouts更为常见:http://developer.android.com/guide/topics/ui/layout/gridview.html

答案 2 :(得分:1)

尝试创建" 使用ArrayAdapter创建多列布局"

* 步骤1.插入一个简单的列表视图,您可以将其转换为多列。 * main.xml中

<ListView android:id="@+id/mylist" android:layout_width="wrap_content" android:layout_height="wrap_content">
</ListView>

步骤2.创建一个行格式,该格式将显示在列表中的每一行。 (多列视图)。

file:mylistrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:paddingTop="4dip"
 android:paddingBottom="6dip"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <TextView android:id="@+id/column1"
    android:gravity=”left”
     android:layout_width="50dip"
     android:layout_height="wrap_content"/>
 <TextView android:id="@+id/column2"
    android:gravity=”center”
     android:layout_width="70dip"
     android:layout_height="wrap_content"
     android:layout_weight="1"/>
 <TextView android:id="@+id/column3"
     android:gravity=”right”
     android:layout_width="60dip"
     android:layout_height="wrap_content"
       android:layout_weight="1"/>
</LinearLayout>

步骤3.使用SimpleAdapter配置与这些列的数据绑定

ListView list = (ListView) findViewById(R.id.mylist);
ArrayList<HashMap<String, String>> mylistData =
               new ArrayList<HashMap<String, String>>();

String[] columnTags = new String[] {"col1", "col2", "col3"};

int[] columnIds = new int[] {R.id.column1, R.id.column2, R.id.column3};
for(int i=0; i<3; i++)
{
HashMap<String,String> map = new HashMap<String, String>();
 //initialize row data
 for(int j=0; j<3; j++)
 {
   map.put(columnTags[j], "row”+i+”col"+j);
 }
 mylistData.add(map);
 }
 SimpleAdapter arrayAdapter =
           new SimpleAdapter(this, mylistData, R.layout.mylistrow,
                         columnTags , columnIds);
 list.setAdapter(arrayAdapter);

答案 3 :(得分:1)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="top_center_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/ic_launcher"
            android:gravity="center"
            android:text="names"
            android:textColor="@android:color/background_dark" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/ic_launcher"
            android:gravity="center"
            android:text="names"
            android:textColor="@android:color/background_dark" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/ic_launcher"
            android:gravity="center"
            android:text="names"
            android:textColor="@android:color/background_dark" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/ic_launcher"
            android:gravity="center"
            android:text="names"
            android:textColor="@android:color/background_dark" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/ic_launcher"
            android:gravity="center"
            android:text="names"
            android:textColor="@android:color/background_dark" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/ic_launcher"
            android:gravity="center"
            android:text="names"
            android:textColor="@android:color/background_dark" />
    </LinearLayout>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="top_center_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

以下是您需要的代码

答案 4 :(得分:0)

考虑使用GridView

当然,LinearLayout将解决您的问题,但始终考虑使用最佳实践或方法来开发任何内容。

您也可以使用this

答案 5 :(得分:0)

您可以使用GridLayoutTableLayout

但我建议你使用TableLayout。