簡體   English   中英

創建自己的自定義視圖而不使用XML

[英]Create own custom View without using XML

我想創建一個包含兩個按鈕而不使用xml的自定義視圖。

我嘗試了這個:

public class ZoomPlate extends LinearLayout {

    private Context context;

    private Button plus;
    private Button minus;

    public ZoomPlate(Context context) {
        super(context);
        init(context);

    }

    public ZoomPlate(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.context = context;     

        LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0);

        plus = new Button(context);
        plus.setText("Plus");
        plus.setLayoutParams(params);

        minus = new Button(context);
        minus.setText("Minus");
        minus.setLayoutParams(params);

        setOrientation(LinearLayout.VERTICAL);


        addView(plus);
        addView(minus);
    }
}

這樣我就可以像按鈕或textview這樣在XML中使用ZoomPlate:

<at.bartinger.zoomplate.ZoomPlate
    android:id="@+id/zoomplate"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />

但是當我嘗試這個時什么也沒顯示

您如何將自定義視圖添加到XML布局? 您需要使用<com.blabla.bla.ViewName android:properties="whatever" />的完整軟件包名稱。

它可能與在主“活動”中設置為內容視圖的內容有關? 在這里檢查: http : //developer.android.com/reference/android/app/Activity.html#setContentView%28int%29

基本上,您必須從布局資源或視圖中進行設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM