編輯:我通過簡單地更改TextView的父ScrollView的背景解決了以下#2。Android標籤問題 - 以編程方式更改內容
我一直在谷歌搜索,但似乎無法找到答案。這應該很簡單。
我的活動包含由Tabbed控件(TabHost,TabWidgets等)組成的片段。每個選項卡上都有一個簡單的滾動TextView。這個想法是有幾個標籤,用戶可以滾動瀏覽不同類別的文本。我已經能夠設置標籤等,但有2個問題,我似乎無法解決。
1)我想不出如何以編程方式(從擁有該片段的活動)更改選項卡的TextView的內容。我已經嘗試在我的主機活動中創建一個FragmentManager,查找片段,將片段投射到我的片段類中,然後在片段類中調用應該更改文本的函數。此功能只是這樣做的:
TextView textView = (TextView)tabHost.getTabWidget().getChildAt(tabNumber).findViewById(R.id.dashboard_fragment_4_textView);
if(textView!=null) textView.setText(text);
這是行不通的; textView始終返回爲空。
2)佈局問題,我相信。每個選項卡上的我的TextView不會填充父項;它只是包裝文本內容。這不是什麼大問題,但我希望TextView的整個屏幕區域都有特定的背景顏色。也許有更好的方法來處理這個?像TextView的背景透明,然後在TextView背後使用正確的顏色?我不知道....我正在接觸。
這裏的XML佈局爲每個標籤:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="left"
android:layout_weight="0" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1">
<ScrollView
android:id="@+id/dashboard_fragment_4_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/dashboard_fragment_4_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Linen"
android:textColor="@color/Black"
android:textSize="20dp"
android:text="TextView" />
</ScrollView>
</FrameLayout>
</LinearLayout>
感謝您的建議!
布萊恩
感謝傑夫。你的評論讓我有一部分的方式。我也需要改變我的XML佈局。我還必須更改我的xml文件,以便爲每個標籤頁分別擁有一個ScrollView/TextView ...現在我想到了這一點。可能有一個更優雅的方式來處理這個問題,但它對我有用。再次感謝! – 2012-01-18 22:33:14