我的自定义控件在界面上显示不出来 这是为什么 老师
这是我自定义的一个控件由两个按钮 和一个editText组成 可是为什么我的控件不能显示出来呢
这是我的Java代码实现一些属性的设置public class MyTopbar extends RelativeLayout{
private String title,leftbutton,rightbutton;
private Button leftBtn,rightBtn;
private EditText editText;
private LayoutParams paramRight,paramsLeft,paramsTitle;
public MyTopbar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyTopbar);
title = ta.getString(R.styleable.MyTopbar_title);
leftbutton = ta.getString(R.styleable.MyTopbar_leftbutton);
rightbutton = ta.getString(R.styleable.MyTopbar_rightbutton);
ta.recycle();
leftBtn = new Button(context);
rightBtn = new Button(context);
editText = new EditText(context);
leftBtn.setText(leftbutton);
rightBtn.setText(rightbutton);
editText.setText(title);
paramsLeft = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftBtn, paramsLeft);
paramRight = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, ViewGroup.LayoutParams.WRAP_CONTENT);
paramRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightBtn, paramRight);
paramsTitle = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsTitle.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(editText,paramsTitle);
}
}
接下来是我的xml 文件 自定义了属性
<resources>
<declare-styleable name="MyTopbar">
<attr name="title" format="string" />
<attr name="leftbutton" format="string" />
<attr name="rightbutton" format="string" />
</declare-styleable>
</resources>
然后是引用我自定义的控件
<com.example.administrator.topbar.MyTopbar
android:layout_width="match_parent"
android:layout_height="104dp"
android:paddingLeft="20dp"
android:paddingBottom="40dp"
android:background="#b4ff66"
app:rightbutton="按钮右"
app:leftbutton="按钮左"
app:title="这是我的第一个自定义控件"
android:layout_gravity="center">
</com.example.administrator.topbar.MyTopbar>