how to display a file in a tab view?

I want to display the content of a file in a TabView. Each value of the file is in an extra line.

With this I am already able to read the file:

public class Tab1Activity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infolayout);
        String line = "";
        TextView text = new TextView(this);
        try {
            BufferedReader b = new BufferedReader(new FileReader("/sdcard/com.unitnode/debug.txt"));
            while ((line = b.readLine()) != null) { // liest zeilenweise aus Datei

                text.setGravity(Gravity.CENTER_VERTICAL);
                Log.d("zeile", "zeile " + line);
                text.setText(line + "r");
                // setContentView(text);
            }
            b.close();
        } catch (IOException e) {
            Log.d("fehler", "fehler ");
        }
        // ViewGroup mContainerView = (ViewGroup) findViewById(tabco);
        // mContainerView.addView(text);
    }
}

And this is the corresponding Layout xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"></FrameLayout>

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test" />
    </LinearLayout>
</TabHost>

How to display the whole file in a TabView?

EDIT: The question is not about how to create Tabs. Just how to display the file in a tab. The Tabs already exists. But my problem is, that I am only able to display one line. I want to display all lines of the file.

Thanks.


I want to display all lines of the file.

You need a minor adjustment in your loop:

text.setGravity(Gravity.CENTER_VERTICAL);// no need to call more than once

StringBuilder sb = new StringBuilder();
int i = 0;
while ((line = b.readLine()) != null) {    
    i++;
    sb.append(line + "n");
}
text.setMaxLines(i);
text.setText(sb.toString());
链接地址: http://www.djcxy.com/p/16654.html

上一篇: Android LinearLayout在xml中查看项目不会填充空白空间

下一篇: 如何在标签视图中显示文件?