2013-08-02 108 views
-2

我有幾個按鈕。我想創建一個單獨的類。當我看見這個按鈕類擴展按鈕

<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); 
    } 
} 

我想創建壓制,以填補新的活動時,三個按鈕。 謝謝。

+0

你能否明確你的問題,這不是很清楚。 –

+0

你面臨什麼問題? –

+0

我希望這些按鈕在我的所有活動人士中都很活躍,我一直在課堂上,而不必在任何活動中創建。 – user2637087

回答

0

由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

希望這有助於

0
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。謝謝!