【发布时间】:2017-08-22 04:36:56
【问题描述】:
我正在尝试使用动态添加的 textview 子项来实现自定义视图。但它没有显示任何结果。没有错误,只是没有显示:
我的班级:
public class CustomPasscodeEntryView extends LinearLayout {
private Context mContext;
private CustomPasscodeEntryView thisView;
public CustomPasscodeEntryView(Context context) {
super(context);
/* same as below */
}
public CustomPasscodeEntryView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
/*same as below*/
}
public CustomPasscodeEntryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thisView = (CustomPasscodeEntryView) inflater.inflate(R.layout.view_passcode_entry,this);
}
public void addDigit(int digit){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final pinView pin = (pinView) inflater.inflate(R.layout.password_bullet_view,null);
pin.setDigit(digit);
thisView.addView(pin, thisView.getChildCount());
pin.setVisibility(VISIBLE);
}
public void deleteDigit(){
if(getChildCount() > 0)
thisView.removeView(thisView.getChildAt(getChildCount()-1));
}
}
class pinView extends TextView {
int digit;
public pinView(Context context) {
super(context);
}
public pinView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public pinView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setText('•');
}
public void setDigit(int digit) {
this.digit = digit;
this.setText(Integer.toString(digit));
}
}
还有我的 XML: view_passcode_entry:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:animateLayoutChanges="true">
</LinearLayout>
password_bullet_view:
<com.github.lockpin.lollipin.lib.views.pinView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="@android:color/white"
android:text="•"
android:textSize="20sp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2.5dp"
/>
编辑:我活动中的视图:
<com.github.lockpin.lollipin.lib.views.CustomPasscodeEntryView
android:id="@+id/custom_passcode_entry_view"
android:layout_width="300dp"
android:layout_below="@id/pin_code_text_view"
android:layout_centerHorizontal="true"
android:layout_height="40dp"/>
我看不出有什么问题,我已经按照在线教程进行了一些更改。我在这里错过了什么吗?或者这显然是错误的?
【问题讨论】:
-
您想在自定义密码视图中做什么?动画什么?
-
我想为每个新数字的输入设置动画(淡入),当输入新字母时,旧字母会转换为项目符号。 (就像在 Pixel / Nexus 锁屏中一样)
-
好的,所以使用自定义
android.text.method.TransformationMethod -
我一开始确实用过。它虽然没有动画,但直接与传递给
TextView的Charsequences和Strings一起工作(或者至少这是我从中推断出来的)。我对Spannables不熟悉,所以决定改为这样做(它有效!:D)
标签: android android-layout android-view android-custom-view layout-inflater