问题描述
我正在使用TextInputLayout,并在其中设置了drawableLeft,当我单击edittext时,标签已经漂浮了,但是我也想使用标签来漂浮左可绘制对象.我该如何实现,请参见所附图片...
I am using TextInputLayout and in which i set a drawableLeft and when i click on edittext the label is already floating up but i want to float the left drawable too with the label. How can i achieve , see the attached image...
现在,当我单击标题"时,只有leble浮动了,但是当我聚焦时,我都希望两者都浮动.在单击之前,它会看起来像服务日期"这样的普通行.
Right now when i click on "Title" only leble is floating up but i want both should be floating "leftdrawable" and "label" when focused. And before to click it will look a normal line like "Service Date".
我的代码在下面.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/input_layout_cardnum"
>
<ImageView
android:id="@+id/imgLeft"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:src="@mipmap/ic_launcher"
android:layout_marginBottom="10dp"
/>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_cardnums"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/cardnumEditTexst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="255"
android:singleLine="true"
android:hint="hello"
android:drawableLeft="@null"
android:paddingLeft="35dp"
/>
</android.support.design.widget.TextInputLayout>
</FrameLayout>
在我的代码中,我在onFocus上设置了一个动画,使其带有标签在顶部
And in my code i'm setting an animation on onFocus so it goes on top with label
final Animation animation = AnimationUtils.loadAnimation(this,R.anim.anims);
final ImageView imgLeft = (ImageView) findViewById(R.id.imgLeft);
final EditText et = (EditText) findViewById(R.id.cardnumEditTexst);
et.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
if (et.getText().length() == 0)
{
imgLeft.startAnimation(animation);
}
} else
{
if (et.getText().length() == 0)
imgLeft.clearAnimation();
}
}
});
然后我的动画会将图像视图放在顶部
And my animation will throw the image view on top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="200"
android:interpolator="@android:interpolator/linear"
>
<scale
android:fromXScale="0%"
android:fromYScale="0%"
android:toXScale="60%"
android:toYScale="60%"
android:pivotX="50%"
android:pivotY="50%"
/>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="100"
android:fromYDelta="0%"
android:toYDelta="-80%"/>
</set>
通过设置该值,现在浮动就可以了,但请参见 paddingLeft ="35dp" ,我想从第0个位置开始,请参见第一张显示"Title"的图像,我的意思是不应该使用padding,但是如果我删除 paddingLeft会导致浮动功能丢失.
And by setting this i got, now floating is okay but see the paddingLeft="35dp", i want to start it from 0th position see in the first image where "Title" is showing, i mean there should not be padding but if i will remove the paddingLeft the floating functionality loss.
推荐答案
尝试一下...
final Animation animation = AnimationUtils.loadAnimation(this, R.anim.anims);
final ImageView imgLeft = (ImageView) findViewById(R.id.imgLeft);
findViewById(R.id.cardnumEditTexst).setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
imgLeft.startAnimation(animation);
} else {
imgLeft.clearAnimation();
}
}
});
和
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fillAfter="true"
android:interpolator="@android:interpolator/linear">
<scale
android:fromXScale="0%"
android:fromYScale="0%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="60%"
android:toYScale="60%" />
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%"
android:startOffset="100"
android:toYDelta="-80%" />
</set>
并像这样设置您的布局...
and set your layout like this...
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/input_layout_cardnum">
<ImageView
android:id="@+id/imgLeft"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:src="@mipmap/ic_launcher"
android:layout_marginBottom="10dp" />
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_cardnums"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/cardnumEditTexst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="255"
android:singleLine="true"
android:hint="hello"
android:drawableLeft="@null"
android:paddingLeft="35dp" />
</android.support.design.widget.TextInputLayout>
</FrameLayout>
这篇关于如何在TextInputLayout中使用(提示)标签浮动左可绘制对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!