Android Tabhost Mayout Вопрос
-
10-10-2019 - |
Вопрос
Это мое первое приложение для Android. Я прошел через приложение «hellotabwidget» на веб -сайте Android, но я не могу понять, как добавить контент на вкладки. Framelayout складывает вещи друг на друга в верхней части слева (от того, что я прочитал). Я добавил пару текстовых обзоров и изображения, но он отображает только последний добавленный элемент. Есть ли способ использовать линейный макет вместо макета рамы? Если нет, то как вы можете разместить несколько просмотров на вкладке? Единственное, что я сделал, отличающееся от примера, это добавить 4 -ю вкладку. В одном из действий вкладок я вставил следующий код, чтобы попытаться получить несколько элементов для отображения:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the About tab");
setContentView(textview);
TextView textview2 = new TextView(this);
textview2.setText("About Test");
setContentView(textview2);
ImageView imgView = new ImageView(this);
imgView.setImageDrawable(getResources().getDrawable(R.drawable.header));
setContentView(imgView);
}
Вот ссылка на пример, которым я следовал:http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
Решение
Какой у вас макет XML -файл? Я использую это, и я могу сложить несколько TextView на каждой вкладке. Табактивность:
<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">
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout 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" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</ScrollView>
</TabHost>
Заявление внутри вкладка:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/textTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout>
Я сам новичок, так что может быть лучший способ сделать это. Это работает для меня, хотя.
Другие советы
Вы не прочитали пример внимательно. Взгляните на пункт № 6; Вы увидите что -то вроде:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
Это то, что вы вкладываете в Oncreate для TabActivity
. Анкет Как видите, у него есть 3 мероприятия. То, что вы делаете, используете только одно действие и устанавливает представление содержимого 3 раза, что, очевидно, неправильно.
Так ... как заставить это работать? Сначала прочитайте еще раз учебник. Во -вторых, создайте одно действие для каждой вкладки, которую вы хотите показать. И используйте приведенную выше модель, чтобы добавить эти действия в ваши TabHost
.