如何在Android中创建布局以水平列出TextView

时间:2016-02-17 08:08:41

标签: android android-layout android-linearlayout

我想创建如下所示的布局:

showcase

Name1,Something1,OtherThing和Name2是TextViews(每个都可以通过不同的动作进行点击),这些是以编程方式添加父版面。有时候有太多元素可以放在一行中。我应该使用哪种布局作为父级,让TextViews水平放置,但有可能“断线”?水平LinearLayout无效。

2 个答案:

答案 0 :(得分:3)

尝试使用FlowLayout

  

FlowLayout是一种在多行中显示其子项的布局   取决于他们的大小。   enter image description here

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

       <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">

        <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="STUFF"
          android:textSize="30dp"/>

       </LinearLayout>
       <com.wefika.flowlayout.FlowLayout                            
        android:layout_width="match_parent"
        android:layout_height="wrap_content"              
        >
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Name1" />

        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Something1" />

        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="OtherThing" />

         <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Name2" />

       </com.wefika.flowlayout.FlowLayout>

           <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">

        <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="MORE STUFF"
         android:textSize="30dp"/>

       </LinearLayout>


     </LinearLayout>

答案 1 :(得分:0)

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="STUFF"
    android:textSize="30dp"/>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Something1" />

</LinearLayout>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OtherThing" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name2" />

</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="MORE STUFF"
    android:textSize="30dp"/>

</LinearLayout>
相关问题