【问题标题】:what is ems and why the editfield size is same (width)?什么是 ems 以及为什么编辑字段大小相同(宽度)?
【发布时间】:2013-09-17 16:21:23
【问题描述】:

我刚开始android开发,所以我有两个问题

  • 首先,为什么编辑字段的大小不会改变它的width

  • 其次,这两行是什么意思

android:layout_weight="1.0"

android:ems="50"

当我在布局表单android:layout_weight= 中使用时,它的值是0.1,为什么?

在现场需要1.0,为什么? ems 是什么?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/login_header" 
         android:gravity="center_horizontal"
         android:textSize="30dp" />

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
         android:padding="10dp" >

         <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/username"
            android:textSize="18dp"/>

           <EditText
            android:id="@+id/username"
            android:layout_width="5dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:ems="50"
            android:inputType="text"/ >

    </LinearLayout>

      <Button
        android:id="@+id/login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/login_header"
        android:layout_margin="10dp"/>

</LinearLayout>

【问题讨论】:

标签: android android-layout


【解决方案1】:

您的 EditText 宽度没有改变,因为您将 layout_weight = 1 赋予 EditText,这意味着 EditText 将占据您 Horizo​​ntal LinearLayout 的全部剩余宽度 在其中绘制。

如果您将在 TextView 和 EditText 中使用 .5 layout_weight。它们都将占据整个宽度的一半。它是一个水平的 LinearLayout,所以重量会影响视图的宽度。所以 Edittext 的宽度是没用的。而在垂直线性布局的情况下,视图的高度将受到 layout_weight 的影响。

    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:padding="10dp" >

      <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/username"
        android:textSize="18dp"
        android:layout_weight=".5" />

       <EditText
        android:id="@+id/username"
        android:layout_width="5dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:ems="50"
        android:inputType="text" >

     </EditText>
</LinearLayout>

Ems:- 是一个印刷术语。有关 ems 的更多信息,请查看this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-22
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多