android - 如何在线性布局中对齐编辑文本

标签 android android-linearlayout

我有一个 ListView ,其中有一个 TextView 、一个编辑文本和一个按钮。我的观点是这样的:

enter image description here

我希望所有的编辑文本都在一行中。我该怎么做?我试过在线性布局中提供 layout_weights,但它似乎对我不起作用。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/posmTypeLL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/posmDeploymentTypeTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Deployment Type"
            android:textSize="@dimen/_11ssp"
            android:layout_weight=".60"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_marginLeft="@dimen/_10sdp"/>

        <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
            android:id="@+id/posmDeploymentTypeSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Select Status"
            android:textSize="@dimen/_10ssp"
            android:layout_gravity="center_horizontal"
            android:layout_weight=".30"
            android:layout_marginTop="@dimen/_10sdp"

            app:met_floatingLabel="normal" />

        <ImageButton
            android:id="@+id/imageButtonRemovePOSMType"
            android:layout_width="@dimen/_20sdp"
            android:layout_height="@dimen/_20sdp"
            android:layout_weight=".10"
            android:layout_marginRight="@dimen/_8sdp"
            android:layout_marginTop="@dimen/_15sdp"
            android:layout_marginLeft="@dimen/_10sdp"
            android:background="@drawable/round_button2"

            app:srcCompat="@drawable/met_ic_close" />

    </LinearLayout>

最佳答案

如果您正在使用 layout_weight 属性,则始终将 layout_width 用作 0dp

请检查下面的示例代码,它运行良好。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/posmTypeLL"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/posmDeploymentTypeTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:gravity="start"
        android:text="Deployment Type"
        android:textSize="@dimen/_11ssp" />

    <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
        android:id="@+id/posmDeploymentTypeSpinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:hint="Select Status"
        android:textSize="@dimen/_10ssp"
        app:met_floatingLabel="normal" />

    <ImageButton
        android:id="@+id/imageButtonRemovePOSMType"
        android:layout_width="@dimen/_20sdp"
        android:layout_height="@dimen/_20sdp"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_8sdp"
        android:background="@drawable/round_button2"
        app:srcCompat="@drawable/met_ic_close" />

</LinearLayout>

关于android - 如何在线性布局中对齐编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521863/

相关文章:

java - 一款使用语音识别来计算说出的单词数的应用程序

android - 如何使用通知 addAction 调用方法或电话号码

java - Android 中的 TableLayout 行和列与 java

java - ArrayIndexOutOfBoundsException 中指定的 "length"是什么?

android - 如何在android中退出应用程序(正常或异常)时执行某些任务?

android - Android 认可哪种 SSL 证书?

android - 如何通过单击按钮添加和删除线性布局

android - GLSurfaceView 父级为空,即使它嵌套在 LinearLayout 中

ListView 中的 Android 进度对话框

android - 如何通过单击图像按钮动态设置编辑文本可编辑?