gridlayout

时间:2017-12-14 06:41:50

标签: android android-layout grid-layout

我在linearlayout内有一个textview imageview和一个gridlayout。我想在column1中使用linearlayout,textview和imageview在行的该列中正确拟合。但图像视图位于textview下方。

必填结果:

enter image description here

我的输出: enter image description here

这是我的代码:

<GridLayout
    android:orientation="horizontal"
    android:rowCount="4"
    android:columnCount="3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gridLayoutSuburb">
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/linearLayout1"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_width="0dp"
        android:layout_columnSpan="1"
        android:layout_columnWeight="1"
        android:background="@color/blue">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/edittext"
            android:id="@+id/select"
            android:textColor="@color/white"
            android:textSize="@dimen/_16sdp"
            android:textColorHint="@color/white"
            android:editable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:hint="Select "
            android:paddingLeft="@dimen/_5sdp"
            android:layout_weight="1"
            android:singleLine="true" />
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:id="@+id/selectImage"
            android:src="@drawable/ic_menu_back" />
    </LinearLayout>
</GridLayout>

我做错了什么?

6 个答案:

答案 0 :(得分:3)

orientation

将您的Linearlayout android:orientation="horizontal"更改为android:orientation="vertical"

使用

 android:orientation="horizontal"

而不是

 android:orientation="vertical"

<强> CODE

<GridLayout
    android:orientation="horizontal"
    android:rowCount="4"
    android:columnCount="3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gridLayoutSuburb">
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/linearLayout1"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_width="0dp"
        android:layout_columnSpan="1"
        android:layout_columnWeight="1"
        android:background="@color/blue">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/edittext"
            android:id="@+id/select"
            android:textColor="@color/white"
            android:textSize="@dimen/_16sdp"
            android:textColorHint="@color/white"
            android:editable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:hint="Select "
            android:paddingLeft="@dimen/_5sdp"
            android:layout_weight="1"
            android:singleLine="true" />
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:id="@+id/selectImage"
            android:src="@drawable/ic_menu_back" />
    </LinearLayout>
</GridLayout>

答案 1 :(得分:2)

替换 LinearLayout的此属性值:

android:orientation="vertical"

android:orientation="horizontal"

答案 2 :(得分:1)

将您的代码更新为此

<GridLayout
android:orientation="horizontal"
android:rowCount="4"
android:columnCount="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridLayoutSuburb">
<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/linearLayout1"
    android:layout_row="0"
    android:layout_column="0"
    android:layout_width="0dp"
    android:layout_columnSpan="1"
    android:layout_columnWeight="1"
    android:background="@color/blue">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/edittext"
        android:id="@+id/select"
        android:textColor="@color/white"
        android:textSize="@dimen/_16sdp"
        android:textColorHint="@color/white"
        android:editable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:hint="Select "
        android:paddingLeft="@dimen/_5sdp"
        android:layout_weight="1"
        android:singleLine="true" />
    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:id="@+id/selectImage"
        android:src="@drawable/ic_menu_back" />
</LinearLayout>

答案 3 :(得分:1)

用户android:orientation="horizontal"并给予android:width =&#34; 0dp&#34;这样文本就会保留在一行中并且不会增加它的宽度。

<GridLayout
android:orientation="horizontal"
android:rowCount="4"
android:columnCount="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridLayoutSuburb">
<LinearLayout
    android:orientation="vertical"
    android:id="@+id/linearLayout1"
    android:layout_row="0"
    android:layout_column="0"
    android:layout_width="0dp"
    android:layout_columnSpan="1"
    android:layout_columnWeight="1"
    android:background="@color/blue">
    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/edittext"
        android:id="@+id/select"
        android:textColor="@color/white"
        android:textSize="@dimen/_16sdp"
        android:textColorHint="@color/white"
        android:editable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:hint="Select "
        android:paddingLeft="@dimen/_5sdp"
        android:layout_weight="1"
        android:singleLine="true" />
    <ImageView
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:id="@+id/selectImage"
        android:src="@drawable/ic_menu_back" />
</LinearLayout>
</GridLayout>

答案 4 :(得分:1)

将线性布局方向更改为

  

机器人:取向=&#34;水平&#34;

<GridLayout
android:orientation="horizontal"
android:rowCount="4"
android:columnCount="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridLayoutSuburb">
<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/linearLayout1"
    android:layout_row="0"
    android:layout_column="0"
    android:layout_width="0dp"
    android:layout_columnSpan="1"
    android:layout_columnWeight="1"
    android:background="@color/blue">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/edittext"
        android:id="@+id/select"
        android:textColor="@color/white"
        android:textSize="@dimen/_16sdp"
        android:textColorHint="@color/white"
        android:editable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:hint="Select "
        android:paddingLeft="@dimen/_5sdp"
        android:layout_weight="1"
        android:singleLine="true" />
    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:id="@+id/selectImage"
        android:src="@drawable/ic_menu_back" />
</LinearLayout>

答案 5 :(得分:1)

将linearLayout1方向垂直更改为水平方向并在其中添加一些边距:

<GridLayout
    android:orientation="horizontal"
    android:rowCount="4"
    android:columnCount="3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gridLayoutSuburb">
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/linearLayout1"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_width="0dp"
        android:layout_columnSpan="1"
        android:layout_columnWeight="1"
        android:background="@color/blue">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/edittext"
            android:id="@+id/select"
            android:textColor="@color/white"
            android:textSize="@dimen/_16sdp"
            android:textColorHint="@color/white"
            android:editable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:hint="Select "
            android:paddingLeft="@dimen/_5sdp"
            android:layout_weight="1"
            android:singleLine="true" />
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="20dp"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:id="@+id/selectImage"
            android:src="@drawable/ic_menu_back" />
    </LinearLayout>
</GridLayout>