我正在为密码字段添加可见性图标。我希望图标位于密码文本的右侧。但是,当我将图像按钮放在TextInputLayout
内时,它将强制该按钮位于密码字段下方。我不能放在右边。
这是活动的结构
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:elevation="2dp"
android:orientation="horizontal"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:weightSum="10"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical"
android:weightSum="5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="top|center"
android:orientation="vertical"
android:textAlignment="center"
android:weightSum="5">
<ImageView
android:id="@+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3"
android:contentDescription="TODO"
app:srcCompat="@drawable/ole_logo" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.7"
android:text="@string/system_name"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="10sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/app_version"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/lyt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="@color/bg_white"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1">
<pl.droidsonroids.gif.GifImageButton
android:id="@+id/syncIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:background="@null"
android:scaleType="fitCenter" />
<ImageButton
android:id="@+id/imgBtnSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@null"
android:paddingRight="10dp"
android:paddingTop="6dp"
app:srcCompat="@drawable/ic_settings_black_24dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5.5"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="40dp"
android:paddingRight="40dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/MyAppTheme.TextInputLayout">
<EditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_name"
android:singleLine="true"
android:textColor="@android:color/background_dark"
android:textColorHint="@android:color/darker_gray"
android:textSize="10sp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/MyAppTheme.TextInputLayout">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/bg_white"
app:srcCompat="@drawable/visibilityicon" />
<EditText
android:id="@+id/input_password"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:textColor="@android:color/background_dark"
android:textColorHint="@android:color/darker_gray"
android:textSize="10sp" />
</android.support.design.widget.TextInputLayout>
<CheckBox
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.7"
android:fontFamily="sans-serif"
android:text="Save Username and Password"
android:textColor="@color/material_drawer_dark_background"
android:textSize="12sp" />
<Button
android:id="@+id/btn_signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"
android:text="@string/btn_sign_in"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
无需添加单独的视图,您可以在TextInputLayout
的以下属性中进行设置以启用密码可见性切换。
app:passwordToggleDrawable="@drawable/another_toggle_drawable"
app:passwordToggleEnabled="true"