我正在尝试动态添加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"/>
我看不出有什么问题,我已经在网上关注了教程并做了一些改动。我在这里错过了什么吗?或者这显然是错的?
答案 0 :(得分:1)
在浏览视图时使用错误的视图ID view_passcode_entry
,该视图是LinearLayout
的实例,并将其强制转换为CustomPasscodeEntryView
。
您只需在this
中拨打addView()
时使用addDigit()
,因为CustomPasscodeEntryView
也是LinearLayout
的实例。因此可以删除布局view_passcode_entry
,因为它不再需要了。
this.addView(pin, this.getChildCount());
但是,如果您要将视图view_passcode_entry
添加为CustomPasscodeEntryView
个实例的子级,那么您还需要添加thisView
private ViewGroup thisView;
...
thisView = (ViewGroup) inflater.inflate(R.layout.view_passcode_entry, this);
this.addView(thisView, 0);
然后如果您想使用view_passcode_entry
作为pinView
的父级,那么您可以使用
thisView.addView(pin, thisView.getChildCount());
答案 1 :(得分:0)
是不是因为你已经在xml中提到过CustomPasscodeEntryView的高度(android:layout_height =&#34; 40dp&#34;)?
你能尝试一下wrap_content吗?