【问题标题】:Android layerlist drawableAndroid 图层列表可绘制
【发布时间】:2018-01-09 21:17:23
【问题描述】:

我正在为应用程序创建登录布局。对于 LinearLayout(id=@+id/lgn_lyt) 的背景,我尝试使其看起来像下图。它在设计编辑器中看起来不错,但在设备中看起来很奇怪。我使用的drawable是矢量图。是不是因为我在layerlist中使用的矢量图编码错误?期待你的答案....

login_lyt.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/lgn_lyt"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="@dimen/usr_nm_lyt_hght"
        android:background="@drawable/usr_lyt_bg"
        android:layout_marginLeft="@dimen/usrnm_lyt_mrgn_left"
        android:layout_marginRight="@dimen/usrnm_lyt_mrgn_right"
        android:layout_marginTop="@dimen/usrnm_lyt_mrgn_top">
        <EditText
            android:id="@+id/usrnme"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/usr_hnt"
            android:background="@android:color/transparent"
            android:layout_marginLeft="@dimen/usrnm_mrgn_left"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/psswrd_lyt"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="@dimen/usr_nm_lyt_hght"
        android:background="@drawable/psswrd_lyt_bg"
        android:layout_marginLeft="@dimen/usrnm_lyt_mrgn_left"
        android:layout_marginRight="@dimen/usrnm_lyt_mrgn_right"
        android:layout_marginTop="@dimen/usrnm_lyt_mrgn_top">
        <EditText
            android:id="@+id/psswrd"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/psswrd_hnt"
            android:inputType="textWebPassword"
            android:background="@android:color/transparent"
            android:layout_marginLeft="@dimen/usrnm_mrgn_left"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginTop="@dimen/usrnm_lyt_mrgn_top"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/login"
            android:text="@string/login"
            android:textColor="#ffffff"
            android:background="@color/colorPrimary"
             />
    </LinearLayout>
</LinearLayout>

usr_lyt_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        >
        <shape
            android:layout_height="match_parent"
            android:shape="rectangle"
            android:thickness="2dp">
            <solid android:color="#10000000"></solid>
        </shape>
    </item>
    <item
        android:gravity="center_vertical|left"
        android:drawable="@drawable/user"
        android:left="15dp"
        >
    </item>
</layer-list>

在设计编辑器中:

在设备中:

【问题讨论】:

  • 为什么它在设计编辑器中看起来不一样?
  • @jobin 实际上你做错了。也许设计编辑器有问题,但设备显示正确的结果。
  • 设计编辑器可能有什么问题

标签: android drawable layer-list


【解决方案1】:

对于图像,您可以在 EditText 中使用 "android:drawableLeft="" " 属性。 无需在 LinearLayout 中使用背景属性

【讨论】:

    【解决方案2】:

    如果他们不提供设计以外的任何功能,为什么不使用 Drawable-left = myImage 和适当的填充添加图像?

    【讨论】:

      【解决方案3】:

      像这样改变你的布局:你需要在你的edittext中使用android:drawableStart:而不是作为背景。您的设备正在显示正确的结果。

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <LinearLayout
          android:id="@+id/lgn_lyt"
          android:layout_gravity="center"
          android:layout_width="match_parent"
          android:layout_height="@dimen/usr_nm_lyt_hght"
          android:layout_marginLeft="@dimen/usrnm_lyt_mrgn_left"
          android:layout_marginRight="@dimen/usrnm_lyt_mrgn_right"
          android:layout_marginTop="@dimen/usrnm_lyt_mrgn_top">
          <EditText
              android:id="@+id/usrnme"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:hint="@string/usr_hnt"
              android:drawableStart="@drawable/usr_lyt_bg"
              android:drawableLeft="@drawable/usr_lyt_bg"
              android:background="@android:color/transparent"
              android:layout_marginLeft="@dimen/usrnm_mrgn_left"
              />
             </LinearLayout>
      
           <LinearLayout
          android:id="@+id/psswrd_lyt"
          android:layout_gravity="center"
          android:layout_width="match_parent"
          android:layout_height="@dimen/usr_nm_lyt_hght"
          android:layout_marginLeft="@dimen/usrnm_lyt_mrgn_left"
          android:layout_marginRight="@dimen/usrnm_lyt_mrgn_right"
          android:layout_marginTop="@dimen/usrnm_lyt_mrgn_top">
          <EditText
              android:id="@+id/psswrd"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:hint="@string/psswrd_hnt"
              android:drawableStart="@drawable/psswrd_lyt_bg"
              android:drawableLeft="@drawable/psswrd_lyt_bg"
              android:inputType="textWebPassword"
              android:background="@android:color/transparent"
              android:layout_marginLeft="@dimen/usrnm_mrgn_left"
              />
          </LinearLayout>
          <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:gravity="center"
          android:layout_marginTop="@dimen/usrnm_lyt_mrgn_top"
          >
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/login"
              android:text="@string/login"
              android:textColor="#ffffff"
              android:background="@color/colorPrimary"
              />
      </LinearLayout>
      </LinearLayout>
      

      【讨论】: