簡體   English   中英

我有:setContentView(cv)但需要:setContentView(R.layout.arc_timer_activity)-不起作用

[英]I Have: setContentView(cv) but Need: setContentView(R.layout.arc_timer_activity) - does not work

我有個問題。 我想使用布局來設計此ArcTimerActivity。 當我將setContentView(cv)更改為setContentView(R.layout.arc_timer_activity)它不起作用。 (我不是程序員,所以對我來說並不容易)

public class ArcTimerActivity extends Activity implements Runnable, View.OnClickListener, View.OnLongClickListener {

    private ArcTimerView cv = null;
    private Thread t = null;
    private boolean run_thread = false;
    private int run_thread_fps = 50; // Max 1000

    private IntervalSession is;

    private final int MENU_PREFERENCES = 15;

    //@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);


        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        is = new IntervalSession();
        restorePreferences();

        cv = new ArcTimerView(this, is);

        cv.setOnClickListener(this);
        cv.setOnLongClickListener(this);

        restoreRunningState(getLastNonConfigurationInstance());

        setContentView(cv);
    }


   ....................

如果使用setContentView(R.layout.arc_timer_activity)-沒有錯誤,只是黑色屏幕。 Maby我需要添加或更改以在布局中顯示“活動”嗎?

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
 android:layout_height="fill_parent"
android:orientation="vertical"

 >

 <View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></View>

  </LinearLayout>

首先,為要在其中添加cv(或IntervalSession())的xml文件中的View給出一個特定的ID。

現在setContentView(R.layout.arc_timer_activity);

現在,如下定義資源視圖的對象:

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
 android:layout_height="fill_parent"
android:orientation="vertical"

 >

 <View
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></View>

  </LinearLayout>

現在在Jva文件中為該ID提供資源

View myView = (View) findViewById(R.id.myView);

現在,創建要添加的視圖的對象。

cv = new ArcTimerView(this, is);

並將其添加到您的視圖。

myView.add(cv); // or myView.addView(cv);
invalidate();

這樣,您可以將任何視圖動態添加到資源布局。

隨時發表評論。

更新

我已按照您的要求實施了我的項目演示之一。 請參見下面的代碼段。 MyView是我添加到的呼叫。 專心吧。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(new MyView(this)); // Edited
    statusString="testing Post For Application Development"; 
    setContentView(R.layout.main);
    display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    drawingLayout = (RelativeLayout)findViewById(R.id.drawingLayout);
    System.out.println("The Layout is: "+drawingLayout);
    myView = new MyView(this);
    drawingLayout.addView(myView);

    display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    // Setting mPaint 
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(7);

在上面的代碼中,MyView是我要添加到布局中的類。 請參考該代碼,您將了解我如何將MyView類對象添加到布局中。

希望這段代碼對您有所幫助。

暫無
暫無

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

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