类扩展按钮
问题描述:
我有几个按钮。我想创建一个单独的类。当我看见这个按钮类扩展按钮
<com.example.shoping.SettingsP.CustomButtons
android:layout_width="110dp"
android:layout_height="60dp"
android:text="Settings"
android:id="@+id/SettingsButton"
android:background="#010101"
android:textColor="#ffffff"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentRight="true"
/>
和类
public class CustomButtons extends Button {
public CustomButtons(Context context) {
super(context);
}
public CustomButtons(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomButtons(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
我想创建压制,以填补新的活动时,三个按钮。 谢谢。
答
由Alex给出的建议是一个很好的办法,或者你可以创建你把这些按钮包括此布局中的所有其他布局以这种方式
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/your_layout_with_buttons" />
一个xml布局另一个想法可能是这个一: 你可以创建一个类(例如MasterActivity)扩展Activity。在以这种方式这个类覆盖的setContentView
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
Button button1=new Button(this);
Button button2=new Button(this);
Button button3=new Button(this);
/**
* set your buttons layout params as you need
*/
((ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content)).addView(button1);
((ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content)).addView(button2);
((ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content)).addView(button3);
}
getWindow().getDecorView().findViewById(android.R.id.content)
给你的根视图,它必须是一个ViewGroup中(如的LinearLayout,RelativeLayout的等)
做这样当你在你活动调用(必须扩展MasterActivity)setContentView,将显示视图,并根据LayoutParams添加按钮。 您还可以设置根据您的根视图的使用insance of
希望这有助于
答
protected void onFinishInflate() {
Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(this);
Button b2 = (Button)findViewById(R.id.b2);
b2.setOnClickListener(this);
Button b3 = (Button)findViewById(R.id.b3);
b3.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()){
...........
}
}
所以我做到了类型不同的LayoutParams。谢谢!
你能否明确你的问题,这不是很清楚。 –
你面临什么问题? –
我希望这些按钮在我的所有活动人士中都很活跃,我一直在课堂上,而不必在任何活动中创建。 – user2637087