Android 台灣中文網

標題: 關於兩個View同時顯示?? [打印本頁]

作者: attente    時間: 2015-6-9 15:17
標題: 關於兩個View同時顯示??
本帖最後由 attente 於 2015-6-9 15:18 編輯

如題
我要兩個View在同一個頁面顯示
用ViewGroup來操作

先不說同時顯示,單獨顯示,經過ViewGroup
有一個是自己設計的.xml
怎麼用都無法顯示(空白)

附上部分程式碼

        final View chart = ChartFactory.getLineChartView(this, dataset, renderer);                       
                //這個可以正常顯示                       
                //這個是achartengine的折線圖
                                                                                                                                                                       
        final View lineChart =getLayoutInflater().inflate(R.layout.activity_line_chart, null);               
                //這個會顯示空白,這是自己的.xml

        ViewGroup VG1 = new ViewGroup(this) {
            @Override
            protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
                //chart.layout(left, top, right, bottom);                                                //單獨顯示
                //lineChart.layout(left, top, right, bottom);                                        //單獨顯示
                chart.layout(left, top, right, top+bottom/2);                                                               
                top += bottom;
                lineChart.layout(left, top, right, top+bottom/2);
            }
        };

        VG1.addView(lineChart);
        VG1.addView(chart);
        setContentView(VG1);


如果兩個分開單獨放在ViewGroup裡面一樣是.xml那個無法顯示
兩個不是都已經變成View,放在ViewGroup裡因該都可以顯示(先不說同時顯示)
那為什麼那個.xml轉View的都無法顯示?


但是如果不經過ViewGroup就可以顯示(這是一定只會顯示一個,因為只能setContentView最後一個),如下

        View chart = ChartFactory.getLineChartView(this, dataset, renderer);                       
                                                                                                                                                                       
        View lineChart =getLayoutInflater().inflate(R.layout.activity_line_chart, null);               

        setContentView(chart);
        //setContentView(lineChart);






請問是我哪個部份搞錯觀念了?
怎麼ViewGroup都無法顯示第2個View
想了好幾天都想不到.....





歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1