0

シンプルなタイマーアプリを書こうとしています。クロノメーターを拡張するカウンタークラスを作成しています。とにかく、カスタム クロノメーターを onCreate() でレイアウトに追加すると、描画されません。後で別のタブに切り替えてから「カウンターアップタブ」に戻ったときにのみ描画されます。その後、それはとどまります。基本的に、どうやって描くのですか?invalidate() を呼び出してみましたが、何も機能していないようです。

問題のコードは次のとおりです。

これは私の onCreate() の中にあります。カスタム Chronometer を linLay という線形レイアウトに追加しています。

    public class TimerMain extends Activity {

    // Handle to the tabhost
    public TabHost tabHost;

    // Handle to the tab widget (this is the view that has the two tabs)
    public TabWidget tabWidget;

    // Handle to the LinearLayout inside the ScrollView
    public LinearLayout linLay;

    // Spec used to create the tabs
    public TabSpec spec;

    // Up Counter handle
    public UpCounter myUpCounter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the view to the main layout (see main.xml)
        setContentView(R.layout.main);

        // Get the handle to the tabhost in the layout and set it up
        tabHost = (TabHost) this.findViewById(R.id.my_tabhost);
        tabHost.setup();

        // Get the handle to the TabWidget that is in the 
        // xml file through the TabHost handle
        tabWidget = tabHost.getTabWidget();

        // Get the handle to the relative layout that is inside of the frame
        // that is used for the tab contents
        linLay = (LinearLayout) this.findViewById(R.id.LinLay);

        // Create the Count Up tab and leave it blank
        spec = tabHost.newTabSpec("CU");
        spec.setIndicator("Count Up");
        spec.setContent(R.id.ScrLay);
        tabHost.addTab(spec);

        // Create the Count Down tab and leave it blank
        spec = tabHost.newTabSpec("CD");
        spec.setIndicator("Count Down");
        spec.setContent(R.id.ScrLay);
        tabHost.addTab(spec);

        // Set up the click listeners for each tab
        initTabClickListen();

        // Start with the Count Up tab selected
        tabHost.setCurrentTab(0);

        // Initialize the first counters
        myUpCounter = initFirstCounters(this.getApplicationContext());        
        myUpCounter.start();
        myUpCounter.setBase(SystemClock.elapsedRealtime());
        //Add the first UpCounter to the scroll view
        linLay.addView(myUpCounter);

    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    private UpCounter initFirstCounters(Context context) {
        UpCounter tempCt;
        // Create the first counter
        tempCt = new UpCounter(context);
        tempCt.setFormat("Initial Format: %s");
        return tempCt;

    }

    // Set up the click listeners for each tab
    private void initTabClickListen() {

        // Set up the listener for the Count Up Tab
        tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                // Detach all views from the layout
                // and add the views for this tab.
                linLay.removeAllViews();
                linLay.addView(myUpCounter);


                // Set the current tab to Count Up
                tabHost.setCurrentTab(0);
            }
        });

        // Set up the listener for the Count Down Tab
        tabWidget.getChildAt(1).setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                // Detach all views from the layout
                // and add the views for this tab.
                linLay.removeAllViews();
                //scrLay.addView(v2);

                // Set the current tab to Count Down
                tabHost.setCurrentTab(1);
            }
        });

    }
}

// Custom Counter Class
class UpCounter extends Chronometer{

    public UpCounter(Context context) {
        super(context);

    }

}

最後に、見たい場合に備えて .xml を次に示します。また、リニアレイアウトを削除し、代わりにスクロールビューを使用してクロノメーターをホルストすることでこれを試しましたが、同じ問題があります。

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
         <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ScrollView 
                android:id="@+id/ScrLay"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <LinearLayout 
                    android:id="@+id/LinLay"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                </LinearLayout>

             </ScrollView>

        </FrameLayout>
    </LinearLayout>
</TabHost>

アプリケーションの起動時にこのカウンターを表示するにはどうすればよいですか?

4

2 に答える 2

1

この開発者のデモをご覧ください。

ここでは、タブ ベースのアプリケーションの作成方法を学習しました。

次に、アクティビティを作成し、それをタブホストに設定します。そして、そのアクティビティでクロノメーターを作成します。

解決しない場合はお知らせください。

于 2012-06-01T13:33:40.403 に答える
0

私はそれを考え出した。

何らかの理由で、これを onCreate() のコードに追加すると:

// Start with the Count Up tab selected
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(0);

それはすべて今すぐ動作します!

于 2012-06-02T05:06:33.547 に答える