Android 对齐密码切换图标

Android 对齐密码切换图标,android,android-layout,Android,Android Layout,如何定位和对齐可拖动到textinputtext右侧的密码切换开关 <com.google.android.material.textfield.TextInputLayout android:id="@+id/passwordTextInputLayout" android:layout_width="0dp" android:layout_height="wrap_content" styl

如何定位和对齐可拖动到
textinputtext
右侧的密码切换开关

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/passwordTextInputLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            style="@style/DefaultTextInputLayout"
            android:paddingStart="@dimen/activity_default_margin"
            android:paddingEnd="@dimen/activity_default_margin"
            app:passwordToggleEnabled="true"
            app:passwordToggleDrawable="@drawable/select_show_password"
            app:passwordToggleTint="#AFB5C0"
            app:errorEnabled="true" 
            app:layout_constraintBottom_toTopOf="@+id/rememberPasswordCheckBox"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"   
            app:layout_constraintTop_toBottomOf="@+id/emailTextInputLayout" >

        <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/passwordEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_password"
                android:textSize="16sp"
                android:inputType="textPassword" />

</com.google.android.material.textfield.TextInputLayout>

我需要将密码开关置于图像上类似的
textinputtext
右侧(红色箭头指向的位置)


下面是我的工作代码

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/input_layout_password"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:hint="Password"
                app:layout_constraintTop_toBottomOf="@id/input_layout_username"
                app:passwordToggleEnabled="true">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/edtxt_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="actionNext"
                    android:inputType="textPassword"
                    android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>

下面是我的输出

我想这会有帮助

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/input_layout_password"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:passwordToggleEnabled="true">

    <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtxt_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:imeOptions="actionNext"
            android:paddingTop="10dp"
            android:paddingBottom="20dp"
            android:paddingStart="10dp"
            android:inputType="textPassword"
            android:drawableRight="@drawable/ic_visibility_off"
            android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>


我用负片填充法解决了这个问题,也许这不是一个好的练习,但我想不出如何用好的练习来解决这个问题

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/passwordTextInputLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            style="@style/DefaultTextInputLayout"
            android:paddingStart="@dimen/activity_default_margin"
            android:paddingEnd="@dimen/activity_default_margin"
            app:passwordToggleEnabled="true"
            app:passwordToggleDrawable="@drawable/select_show_password"
            app:passwordToggleTint="#AFB5C0"
            app:errorEnabled="true"
            app:layout_constraintBottom_toTopOf="@+id/rememberPasswordCheckBox"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/emailTextInputLayout" >

        <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/passwordEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_password"
                android:textSize="16sp"
                android:paddingEnd="-10dp"
                app:setDebugText="@{password}"
                android:inputType="textPassword" />

    </com.google.android.material.textfield.TextInputLayout>

您可以尝试以下方法:

1) MainActivity.class:

public class MainActivity extends AppCompatActivity {

private final String TAG = MainActivity.class.getSimpleName();
private TextInputLayout textInputLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textInputLayout = (TextInputLayout) findViewById(R.id.passwordTextInputLayout);

    /**
     * By inspecting the source code of TextInputLayout, one can find that:
     *
     * - edit text (called editText) and show password (
     * called startIconView) views are both placed inside
     * a frame layout (called inputFrame).
     * - edit text view is the first child of the frame layout
     * and that the show password view is the third child of this layout.
     *
     */
    FrameLayout inputFrame = null;
    TextInputEditText editText = null;
    CheckableImageButton startIconView = null;

    for (int i = 0; i < textInputLayout.getChildCount(); i++) {
        View child = textInputLayout.getChildAt(i);
        if (child instanceof FrameLayout) {
            inputFrame = (FrameLayout) child;
            for (int j = 0; j < inputFrame.getChildCount(); j++) {
                View child1 = inputFrame.getChildAt(j);
                if (child1 instanceof TextInputEditText) {
                    editText = (TextInputEditText) child1;
                } else if (child1 instanceof CheckableImageButton) {
                    if (j == 2) {
                        startIconView = (CheckableImageButton) child1;
                    }
                }
            }
            break;
        }
    }

    /**
     *
     * - relocate editText and startIconView inside inputFrame.
     * In our case we want startIconView to be located to the
     * right of editText
     *
     **/
    if (inputFrame != null) {

        RelativeLayout rl = new RelativeLayout(MainActivity.this);
        FrameLayout.LayoutParams params_rl = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        rl.setLayoutParams(params_rl);

        if (editText != null &&
                startIconView != null) {

            inputFrame.addView(rl);
            inputFrame.removeView(startIconView);
            inputFrame.removeView(editText);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.ALIGN_PARENT_END);
            startIconView.setLayoutParams(params);
            rl.addView(startIconView);

            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params1.addRule(RelativeLayout.ALIGN_PARENT_START);
            params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params1.addRule(RelativeLayout.LEFT_OF, startIconView.getId());
            params1.addRule(RelativeLayout.START_OF, startIconView.getId());
            editText.setLayoutParams(params1);
            rl.addView(editText);

        }
    }
}
}
public类MainActivity扩展了AppCompatActivity{
private final String TAG=MainActivity.class.getSimpleName();
私有文本输入布局文本输入布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textInputLayout=(textInputLayout)findViewById(R.id.passwordTextInputLayout);
/**
*通过检查TextInputLayout的源代码,可以发现:
*
*-编辑文本(称为editText)并显示密码(
*称为startIconView)的视图都放置在内部
*框架布局(称为inputFrame)。
*-编辑文本视图是框架布局的第一个子视图
*显示密码视图是此布局的第三个子视图。
*
*/
FrameLayout inputFrame=null;
textInputItemText-editText=null;
CheckableImageButton startIconView=null;
对于(int i=0;i
2) activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
tools:context=".MainActivity">

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/passwordTextInputLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true"
    android:layout_marginTop="100dp"
    app:passwordToggleDrawable="@drawable/select_show_password"
    app:passwordToggleTint="#AFB5C0"
    app:errorEnabled="true">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="123"
        android:text=""
        android:background="@color/colorPrimary"
        android:inputType="textPassword" />

</com.google.android.material.textfield.TextInputLayout>


<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/passwordTextInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true"
    android:layout_below="@id/passwordTextInputLayout2"
    app:passwordToggleDrawable="@drawable/select_show_password"
    app:passwordToggleTint="#AFB5C0"
    app:errorEnabled="true">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="123"
        android:text=""
        android:background="@color/colorPrimary"
        android:inputType="textPassword" />

</com.google.android.material.textfield.TextInputLayout>

</RelativeLayout>

3) 结果:


但它不是向右对齐密码开关,我无法更改样式。你的意思是,你想在左侧隐藏密码取消隐藏图标?像可绘制的填充,但不移动文本。我需要将“眼睛”向右移动,而不是移动线条或提示。我“解决”它设置负填充,但我认为这是不好的