簡體   English   中英

如何在水平LinearLayout中正確對齊TextView和EditText?

[英]How can I properly align a TextView and EditText in a horizontal LinearLayout?

我正在嘗試使我的EditText視圖EditText 但是我似乎無法正確EditText它們,也無法增加EditText的大小。 最初,我使用RelativeLayout進行了嘗試,它可以使我准確地放置東西,但尺寸仍然很小。 我以為,如果我切換到水平LinearLayout則可以使用layout_weight調整兩個字段,而EditText會自動填充該空間,但事實並非如此。 有什么建議嗎? 當在屏幕上顯示時,此自定義視圖將添加到垂直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:padding="1dp"
    android:weightSum="1"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:gravity="center_vertical"
        android:textColor="@color/ws_blue"
        android:background="#00ffff"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Settings Item: "
        android:id="@+id/itemLabel" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.4"
        android:textColor="@color/ws_blue"
        android:textColorHint="@color/ws_hint"
        android:background="#ffff00"
        android:id="@+id/et_item_value" />

</LinearLayout>

Android Studio預覽:

Android Studio預覽版

我手機上的實際屏幕截圖:

手機屏幕截圖

在此處輸入圖片說明 這是您的代碼:以下代碼與顯示屏幕截圖的布局文件相同。

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp">
    <TableRow>
        <TextView
            android:layout_weight="1"
            android:gravity="center"
            android:text="Device ID:"
            android:textColor="@android:color/holo_green_dark"
            android:textStyle="bold" />
        <TextView
            android:layout_weight="1"
            android:text="00:00:00:00:00:00"
            android:textColor="@android:color/holo_green_dark"
            android:textStyle="bold" />
    </TableRow>
    <TableRow android:layout_marginTop="8dp">
        <TextView
            android:layout_weight="1"
            android:text="Wifi Presets:"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="18sp"
            android:textStyle="bold" />
        <Spinner
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:textColor="@android:color/holo_green_dark"
            android:textStyle="bold" />
    </TableRow>
    <TableRow android:layout_marginTop="8dp">
        <TextView
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright"
            android:text="SSID:"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="18sp"
            android:textStyle="bold" />
        <EditText
            android:layout_marginLeft="16dp"
            android:layout_weight="1"
            android:background="#FFFF00"
            android:hint="SSID"
            android:textColor="@android:color/holo_blue_dark"
            android:textColorHint="@android:color/darker_gray"
            android:textSize="18sp"
            android:textStyle="bold" />
    </TableRow>
    <TableRow android:layout_marginTop="8dp">
        <TextView
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright"
            android:text="Password:"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="18sp"
            android:textStyle="bold" />
        <EditText
            android:layout_marginLeft="16dp"
            android:layout_weight="1"
            android:background="#FFFF00"
            android:hint="Password"
            android:inputType="textPassword"
            android:textColor="@android:color/holo_blue_dark"
            android:textColorHint="@android:color/darker_gray"
            android:textSize="18sp"
            android:textStyle="bold" />
    </TableRow>
    <TableRow android:layout_marginTop="8dp">
        <TextView
            android:layout_weight="1"
            android:text="Security Type:"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Spinner
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:textColor="@android:color/holo_green_dark"
            android:textStyle="bold" />
    </TableRow>
</TableLayout>

嘗試通過提供邊距和填充來放大視圖

<?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"
    android:padding="1dp"
    android:weightSum="1">

    <TextView
        android:id="@+id/itemLabel"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:background="#00ffff"
        android:layout_margin="20dp"
        android:padding="10dp"
        android:gravity="center_vertical"
        android:text="Settings Item: "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/holo_blue_dark"/>

    <EditText
        android:id="@+id/et_item_value"
        android:layout_margin="20dp"
        android:layout_width="0dp"
        android:padding="10dp"
        android:layout_height="match_parent"
        android:layout_weight="0.4"
        android:background="#ffff00"
        android:textColor="@android:color/holo_blue_dark"
        android:textColorHint="@android:color/darker_gray"/>

</LinearLayout>

我認為您最好的選擇是使用GridLayout。 您可以使用

<GridLayout
....
android:columnCount="2">

如果需要更大的EditText,則可以使用

<EditText
.....
android:layout_columnSpan="2"/>

除了建議的答案外,您還可以使用百分比支持庫。 該庫由Google創建,可讓您創建RelativeLayoutFrameLayout ,它具有額外的優勢:定義寬度,高度和邊距的百分比。 要使用它,只需將以下行添加到您的Gradle應用程序文件中:

compile 'com.android.support:percent:23.3.0' // note that the version number 
                                             // might change due to updates

添加此依賴項后,可以像下面這樣使用它:

<android.support.percent.PercentRelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <TextView
         android:id="@+id/itemLabel"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:alignParentLeft="true"
         android:background="#00ffff"
         android:layout_marginLeftPercent="10%"
         android:gravity="center_vertical"
         android:text="Settings Item: "
         android:textAppearance="?android:attr/textAppearanceMedium"
         android:textColor="@android:color/holo_blue_dark"/>

    <EditText
        android:id="@+id/et_item_value"
        app:layout_marginRightPercent="10%"
        android:alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_widthPercent="40%"
        android:background="#ffff00"
        android:textColor="@android:color/holo_blue_dark"
        android:textColorHint="@android:color/darker_gray"/>

 </android.support.percent.PercentRelativeLayout>

請注意,這只是一個示例,您可以根據需要進行修改

有關百分比支持庫的更多信息,請點擊此鏈接

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM