2016-08-22 80 views
0

我想设计EditText密码显示密码imageView。但是我不能正确设计它,因为我看不到ImageView,因为EditText's layout_width="fill_parent"。当我将其更改为wrap_content时,我可以看到我的ImageView,但在UI中看起来很糟糕。是否可以设计它而不给定尺寸?使用显示密码按钮设计密码文本

这是我的代码;

 <android.support.design.widget.TextInputLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="8dp" 
      android:orientation="horizontal" 
      android:layout_marginBottom="8dp"> 

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

      <EditText 
       android:id="@+id/registerPassword" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:hint="@string/register_password" 
       android:inputType="textPassword" /> 
      <ImageView 
       android:id="@+id/fragment_login_password_visibility" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:src="@android:drawable/ic_partial_secure" 
       android:layout_gravity="center" 
       android:clickable="true"/> 

      </LinearLayout> 
     </android.support.design.widget.TextInputLayout> 

编辑:

机器人:layout_weight = 1个来解决问题。现在我可以看到EditText左边的ImageView。但是现在,我TextInputLayout提示不起作用。

Here Hint works

Here Hint does not works

+0

最新的支持库有内置的密码切换的支持。 https://developer.android.com/topic/libraries/support-library/revisions.html#rev24-2-0 – Budius

回答

0

第一组LinearLayout “1” android:weightSum。然后,分别将EditTextandroid:layout_widthandroid:layout_weight设置为“0dp”和“1”。

+0

请看我的编辑,更改提示后不适用于密码字段 – goGud

+0

这些更改与'EditText'提示无关。 –

+0

啊哈,那是因为LinearLayout。是否有可能使用另一个选项而不是linearlayout horizantal orientation? – goGud

0

你尝试使用重量?

android:layout_weight="1"

android:layout_width="0dp"android:layout_width="wrap_content"

<android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" 
     android:orientation="horizontal" 
     android:layout_marginBottom="8dp"> 

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

     <EditText 
      android:id="@+id/registerPassword" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:hint="@string/register_password" 
      android:inputType="textPassword" /> 

     <ImageView 
      android:id="@+id/fragment_login_password_visibility" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:src="@android:drawable/ic_partial_secure" 
      android:layout_gravity="center" 
      android:clickable="true"/> 

     </LinearLayout> 
    </android.support.design.widget.TextInputLayout> 
+0

layout_weight解决了这个问题,但现在我失去了TextInputLayout属性... – goGud

+0

请参阅我的编辑 – goGud

0

试试这个:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <EditText 
       android:id="@+id/editText" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:hint="Password" 
       android:singleLine="true" 
       android:textSize="25sp" 
       android:paddingRight="60dp" 
       /> 
      <ImageButton 
       android:id="@+id/showpass" 
       android:layout_marginLeft="-60dp" 
       style="?android:buttonBarButtonStyle" 
       android:paddingBottom="5dp" 
       android:src="@drawable/pass_ic" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
0

我建议您使用passwordToggleDrawable一个TextInputLayout。

<android.support.design.widget.TextInputLayout 
      android:id="@+id/textInputPasswordLayout" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      app:passwordToggleContentDescription="Show" 
      app:passwordToggleDrawable="@drawable/ic_password_visibility_selector" 
      app:passwordToggleEnabled="true"> 

      <android.support.design.widget.TextInputEditText 
       android:id="@+id/textInputEditTextPassword" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:ems="10" 
       android:fontFamily="sans-serif-light" 
       android:gravity="bottom" 
       android:hint="@string/enter_your_password"/> 


     </android.support.design.widget.TextInputLayout> 

app:passwordToggleDrawable="@drawable/ic_password_visibility_selector"分配您绘制