1

我正试图平衡我的EditText的意见。 但我似乎无法让他们排队正确,我不能增加EditText的大小。起初,我用RelativeLayout试了一下,这让我能够准确地定位事物,但尺寸依然不足。我想如果我切换到水平LinearLayout那么我可以用layout_weight调整这两个字段,而EditText会自动填满这个空间,但似乎并非如此。有什么建议吗? 在屏幕上显示时,此自定义视图被添加到垂直LinearLayout如何正确对齐水平LinearLayout中的TextView和EditText?

我的布局文件:

<?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 Preview

从我的手机

实际截图:

Phone Screenshot

+0

设置宽度0dp为textview和edittext当你把重量和余量留下来也编辑文本 –

+0

@SathishKumarJ谢谢,我会检查出'TableLayout' –

+0

@ Er.Arjunsaini这看起来正确的Android预览屏幕,但当我运行它时,它仍然与我上面的屏幕截图一样。 –

回答

4

enter image description here这里设置更columnsCount和跨度是你的代码: 下面的代码是一样的,你必须显示布局文件屏幕截图。

<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> 
1

试图通过给予边距和填充到放大查看

<?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> 
1

我认为你最好的选择是使用GridLayout。您可以使用

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

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

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

除了建议的答案,您还可以使用百分比支持库。这个由Google创建的图库使您能够创建RelativeLayoutFrameLayout,并具有额外优势:定义宽度,高度和边距的百分比。要使用它,只需添加下面一行到你的摇篮app文件:

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> 

请注意,这只是一个样本,您可以根据您的需要

有关百分比支持库的详细信息进行修改,请点击此link

希望THI s帮助:)