Android 如何在选项卡视图中显示文件?
我想在Android 如何在选项卡视图中显示文件?,android,file,Android,File,我想在选项卡视图中显示文件的内容。文件的每个值都在一个额外的行中 有了这个,我已经能够读取文件了: public class Tab1Activity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.infolayout); Stri
选项卡视图中显示文件的内容。
文件的每个值都在一个额外的行中
有了这个,我已经能够读取文件了:
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);
}
}
这是相应的布局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>
如何在选项卡视图中显示整个文件?
编辑:问题不在于如何创建选项卡。如何在选项卡中显示文件。这些选项卡已经存在。但我的问题是,我只能显示一行。我想显示文件的所有行
谢谢
我想显示文件的所有行
您需要在循环中进行微调:
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());
可能重复的请不要否决我的问题,因为我认为你否决了它,因为创建了标签。但这不是我的问题。我的问题的答案来自SME_DEV