我正在为app创建一个登录布局。对于LinearLayout的背景(id = @ + id / lgn_lyt)我试着让它看起来像下图。它在设计编辑器中看起来很好,但在设备中它看起来很奇怪。我使用的可绘制是一个矢量图像。是因为矢量图像我用它在图层列表中编码错误?期待你的答案....
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>
在设备中:
答案 0 :(得分:0)
您可以使用&#34; android:drawableLeft =&#34;&#34; &#34; EditText中的属性。 无需在LinearLayout中使用背景属性
答案 1 :(得分:0)
如果他们不提供除设计之外的任何功能,为什么不使用Drawable-left = myImage和适当的填充来添加图像?
答案 2 :(得分:0)
像这样改变你的布局:你需要在你的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>