GridLayout (not GridView) how to stretch all children evenly

后端 未结 20 1895
独厮守ぢ
独厮守ぢ 2020-11-22 09:35

I want to have a 2x2 grid with a buttons inside. This is only ICS so I am trying to use the new GridLayout given.

Here\'s the XML of my layout:

 <         


        
相关标签:
20条回答
  • 2020-11-22 10:31

    I wanted to have a centered table with the labels right aligned and the values left aligned. The extra space should be around the table. After much experimenting and not following what the documentation said I should do, I came up with something that works. Here's what I did:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" >
    
    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="2"
        android:orientation="horizontal"
        android:useDefaultMargins="true" >
    
        <TextView
            android:layout_gravity="right"
            android:text="Short label:" />
    
        <TextView
            android:id="@+id/start_time"
            android:layout_gravity="left"
            android:text="Long extended value" />
    
        <TextView
            android:layout_gravity="right"
            android:text="A very long extended label:" />
    
        <TextView
            android:id="@+id/elapsed_time"
            android:layout_gravity="left"
            android:text="Short value" />
    </GridLayout>
    

    This seems to work but the GridLayout shows the message:

    "This GridLayout layout or its LinearLayout parent is useless"

    Not sure why it is "useless" when it works for me.

    I'm not sure why this works or if this is a good idea, but if you try it and can provide a better idea, small improvement or explain why it works (or won't work) I'd appreciate the feedback.

    Thanks.

    0 讨论(0)
  • 2020-11-22 10:34

    This is the code for more default application without the buttons, this is very handy for me

    <GridLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:columnCount="1"
       >
       <TextView
           android:text="2x2 button grid"
           android:textSize="32dip"
           android:layout_gravity="center_horizontal" />
    
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal">
           <Space
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_weight="1" />
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Naam" />
           <Space
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_weight="1" />
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="start"
               android:text="@{viewModel.selectedItem.test2}" />
           <Space
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_weight="1" />
       </LinearLayout>
    
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"
           >
           <Space
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_weight="1" />
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Nummer" />
           <Space
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_weight="1" />
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="start"
               android:text="@{viewModel.selectedItem.test}" />
           <Space
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_weight="1" />
       </LinearLayout>
    </GridLayout>
    
    0 讨论(0)
提交回复
热议问题