android - 对齐密码切换图标
问题描述
如何将可绘制的密码切换定位和对齐到右侧TextInputEditText
?
<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>
我需要将密码切换定位在TextInputEditText
图像右侧(红色箭头指向的位置)。
解决方案
您可以尝试以下方法:
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);
}
}
}
}
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) 结果:
推荐阅读
- python - 从 Python 与以太坊智能合约交互
- javascript - 如何在反应js中30秒后从dom中删除usestate元素
- python - 如何让 discord.py 中的机器人响应特定用户?
- dask - 通过 env vars 配置 dask 网关
- node.js - 键入'字符串 | undefined' 不能分配给类型 'string'。类型“未定义”不可分配给类型“字符串”
- css - 在 R 的 slickR 轮播中移动上一个下一个按钮
- node.js - 事件循环 uv__io_poll & uv__run_pending(
- sql - 记录满足多个条件的用户
- ruby - 我无法更改我想用我的 Mac (M1) 运行的 ruby 版本
- jquery - 在 vimeo 时间点触发事件