Android: creating two columns in a linearlayout

后端 未结 7 2260
囚心锁ツ
囚心锁ツ 2020-12-13 02:08


        
相关标签:
7条回答
  • 2020-12-13 02:44

    A potential issue with just having two vertical LinearLayouts for you columns is that nothing ensures rows will line up if row heights are variable. TableLayout is best for this, and also gives you a bunch of control on how columns shrink or grow to fill available space.

    The link has changed since @santhosh-shettigar posted.
    Guide: https://developer.android.com/guide/topics/ui/layout/grid.html
    Reference: http://developer.android.com/reference/android/widget/TableLayout.html

    0 讨论(0)
  • 2020-12-13 02:49

    I hope it will helpful to you.

    Try this Code..

    <LinearLayout 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:orientation="horizontal">
    
    
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#00FF00"
            android:paddingRight="90dp"
            android:orientation="vertical" >
    
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/b1"
                android:text="Button 1"/>
    
        </LinearLayout>
    
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF00FF"
            android:orientation="vertical" >     
    
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/b2"
                android:text="Button 2"/>
    
        </LinearLayout>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-13 02:51

    Yeah, this one is confusing. Even though the width of LinearLayout is set to fill_parent, it still only takes the minimum width necessary. You need to set the 2nd TextView to fill_parent and then its gravity to right:

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Street"/>
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="456546546"
            android:gravity="right" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-13 02:58

    You should use android:layout_weight attribute. Here is an example:

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Street" 
            android:layout_gravity="left"
            android:background="#88FF0000"/>
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="456546546"
            android:layout_gravity="right" 
            android:background="#8800FF00"/>
    
    </LinearLayout>
    

    enter image description here

    0 讨论(0)
  • 2020-12-13 03:02

    And if you need a gap between buttons :

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <Button
    
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:text="Via SMS" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.05" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:text="Diaporama" />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-13 03:03

    If u want to have multiple rows in each columns u can use Table Layout

    0 讨论(0)
提交回复
热议问题