Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:哪种布局?_Android_Layout_Android Linearlayout_Grid Layout - Fatal编程技术网

Android 安卓:哪种布局?

Android 安卓:哪种布局?,android,layout,android-linearlayout,grid-layout,Android,Layout,Android Linearlayout,Grid Layout,我想创建一个ListView行,如下所示: aaaa bbbb ............. c d e aaaa bbbb ............. c d e aaaa bbbb ............. c d e aaaa bbbb ............. c d e aaaa bbbb ............. c d e aaaa bbbb ............. c d e 其中: aaaa和bbbb是固定大小的

我想创建一个ListView行,如下所示:

aaaa bbbb ............. c d e
aaaa bbbb ............. c d e
aaaa bbbb ............. c d e
aaaa bbbb ............. c d          e
aaaa bbbb ............. c d          e
aaaa bbbb ............. c d          e
其中: aaaa和bbbb是固定大小的列 .... 是一个可变大小的文本字符串 c、 d&e是每幅30x30像素的图像

哪一种布局是用于此的最佳布局?我想要的是。。。。文本字符串以使用所有剩余空间,但所有其他列保持固定宽度

我尝试使用LinearLayout,但无法使c、d和e对齐,除非我为。。。。。。但我不想为

GridLayout几乎做到了这一点,但它看起来是这样的:

aaaa bbbb ............. c d e
aaaa bbbb ............. c d e
aaaa bbbb ............. c d e
aaaa bbbb ............. c d          e
aaaa bbbb ............. c d          e
aaaa bbbb ............. c d          e
这是我的GridLayout XML,但我不承诺使用GridLayout。如果可能的话,我还希望避免在运行时设置列宽

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="6"

    android:orientation="horizontal" >

    <TextView
        android:id="@+id/lv_artist"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:text="Artist"
        android:textColor="#000" />

    <TextView
        android:id="@+id/lv_album"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Album"
        android:textColor="#000" />

    <TextView
        android:id="@+id/lv_tune"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Tune"
        android:textColor="#000" />

    <ImageView
        android:id="@+id/ibAddPlaylist"
        android:gravity="right"
        android:layout_gravity="right"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/clipboard30x30grey" />

    <ImageView
        android:id="@+id/ibLikeHate"
        android:gravity="right"
        android:layout_gravity="right"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/unchecked" />

    <ImageView
        android:id="@+id/ivPlay"
        android:gravity="right"
        android:layout_gravity="right"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/play_30x30" />    
</GridLayout>


您是否尝试过在其上使用
android:weightSum
并在其子视图上使用
android:layout\u weight

您是否尝试过在其上使用
android:weightSum
并在其子视图上使用
android:layou weight
视图

layout\u重力
layout\u重量
将起作用,请参见Android的或上的
线性布局

以下代码应该可以工作:

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

    <TextView
        android:id="@+id/lv_artist"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:text="Artist"
        android:textColor="#000" />

    <TextView
        android:id="@+id/lv_album"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Album"
        android:textColor="#000" />

    <!--
      ~ Use android:layout_weight so that this view will take up all the space
      ~ remaining when no other view has specifed its weight.
     -->
    <TextView
        android:id="@+id/lv_tune"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:text="Tune"
        android:textColor="#000" />

    <!-- 
      ~ android:gravity is for the gravity in the View itself, while
      ~ android:layout_gravity is for the gravity of the View in it's
      ~ ViewGroup(container), in this case it may not be what you intended to
      ~ do, so I removed it for you.
     -->
    <ImageView
        android:id="@+id/ibAddPlaylist"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:src="@drawable/clipboard30x30grey" />

    <ImageView
        android:id="@+id/ibLikeHate"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:src="@drawable/unchecked" />

    <ImageView
        android:id="@+id/ivPlay"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:src="@drawable/play_30x30" />    
</LinearLayout>

layout\u gravity
layout\u weight
将发挥作用,请参见Android的或
LinearLayout

以下代码应该可以工作:

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

    <TextView
        android:id="@+id/lv_artist"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:text="Artist"
        android:textColor="#000" />

    <TextView
        android:id="@+id/lv_album"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Album"
        android:textColor="#000" />

    <!--
      ~ Use android:layout_weight so that this view will take up all the space
      ~ remaining when no other view has specifed its weight.
     -->
    <TextView
        android:id="@+id/lv_tune"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:text="Tune"
        android:textColor="#000" />

    <!-- 
      ~ android:gravity is for the gravity in the View itself, while
      ~ android:layout_gravity is for the gravity of the View in it's
      ~ ViewGroup(container), in this case it may not be what you intended to
      ~ do, so I removed it for you.
     -->
    <ImageView
        android:id="@+id/ibAddPlaylist"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:src="@drawable/clipboard30x30grey" />

    <ImageView
        android:id="@+id/ibLikeHate"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:src="@drawable/unchecked" />

    <ImageView
        android:id="@+id/ivPlay"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:src="@drawable/play_30x30" />    
</LinearLayout>


weightSum
完全没有用处。安卓会自动计算,我知道。我想说的是,也许固定的列有固定的宽度,没有权重,而扩展的列有一个。@DreamingCode的答案就是我想的。
weightSum
完全没有用。安卓会自动计算,我知道。我想说的是,也许固定的列有固定的宽度,没有权重,而扩展的列有一个。@DreamingCode的答案就是我的想法。谢谢。这就成功了。也很简单。我已经有一段时间没有仔细研究过布局了,但上一次我不得不在代码中计算和设置列宽,这是一件很痛苦的事情——尽管是GridLayouts。我已经接受了你的答案-谢谢你的快速回答。谢谢。这就成功了。也很简单。我已经有一段时间没有仔细研究过布局了,但上一次我不得不在代码中计算和设置列宽,这是一件很痛苦的事情——尽管是GridLayouts。我已经接受了你的答案-谢谢你的快速回答。