2010-06-13 37 views
5

Những gì tôi đang cố gắng làm là tạo ra một bố cục như thế này:Vấn đề với TabHost UI bố trí

 +--------+ 
Search |EditText| [Button] 
     +--------+ 

+---------+ 
|Tab 1 |---------+ 
|   |Tab 2 | 
|---------+---------+-----------------+ 
| ListView Item 1      | 
| ListView Item 2      | 
| ListView Item 3      | 
| ListView Item 4      | 
| ListView Item 5      | 
| ListView Item 6      | 
| ListView Item 7      | 
| ListView Item 8      | 
| ListView Item 9      | 
| ListView Item 10     | 
+-------------------------------------+  

Vì vậy, đó là một TextView nói 'Search', một EditText và một nút.

Sau đó bên dưới các tab - Tab 1 có một ListView, Tab 2 một số nội dung khác. Tôi có thể nhận được một thiết lập TabHost đơn giản với hai tab làm việc tốt nhưng không thể có được ở trên để lay ra đúng cách.

Vấn đề là nó cài đặt và chạy tốt - chỉ bố cục là sai. Tôi đã thử quan điểm có trọng số và như vậy nhưng tôi không thể có được những thứ để vị trí theo sơ đồ ASCII ở trên.

Tôi muốn tạo giao diện người dùng trong trình thiết kế Eclipse và kiểm tra XML từ đó, ngoại trừ trình thiết kế không hoạt động với TabHost. Và DroidDraw dường như không biết gì về TabHost.

Đây là XML:

<?xml version="1.0" encoding="utf-8"?> 

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+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" 
      android:padding="5dp"> 

      <TextView android:id="@+id/SearchTextView" 
       android:text="Search" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="10"> 
      </TextView> 

      <EditText 
       android:text="@+id/SearchEditText" 
       android:id="@+id/SearchEditText" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:singleLine="true" 
       android:layout_weight="10"> 
      </EditText> 

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

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

       <LinearLayout android:id="@+id/Tab1ListLayout" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
       </LinearLayout>  

       <Button android:id="@+id/tab2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="Tab 2 button"/> 

      </FrameLayout>  
     </LinearLayout> 
</TabHost> 

... và đây là Hoạt động:

public class tabtest extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost tabs = (TabHost)findViewById(R.id.TabHost); 
     tabs.setup(); 
     TabHost.TabSpec spec = tabs.newTabSpec("tag1"); 
     spec.setContent(R.id.Tab1ListLayout); 
     spec.setIndicator("Tab 1"); 
     tabs.addTab(spec); 
     spec=tabs.newTabSpec("tag2"); 
     spec.setContent(R.id.tab2); 
     spec.setIndicator("Tab 2"); 
     tabs.addTab(spec); 

     EditText et = (EditText)findViewById(R.id.SearchEditText); 
     et.setText("Some text."); 

    } 
+1

Đăng mã trong câu hỏi của bạn để người khác có thể xem những gì bạn đã làm cho đến bây giờ và đưa ra câu trả lời dựa trên đó. – primpap

+0

Tôi sẽ không đề xuất nhà thiết kế Eclipse hoặc AndroidDraw để thiết kế các tab. Làm tốt hơn theo cách thủ công trong xml hoặc trong mã của bạn. Như primalpop đã nói, hãy xác định chính xác độ khó của bạn là gì ("các tab hoạt động tốt nhưng không thể đưa những điều trên vào đúng vị trí"). Có phải về cách áp dụng các độ cao/lề/độ đệm khác nhau cho tab1/2 không? –

+0

Mathias - Tôi đã đề cập đến Eclipse/DroidDraw theo nghĩa rằng tôi thậm chí không thể sử dụng chúng để kiểm tra XML được tạo ra cho những gì nó * nên * trông như thế nào. –

Trả lời

2

Có vẻ như bạn cần phải xem xét vấn đề cơ bản như "layout resources""declaring layout".

Xem layout tricks cho mẹo bố cục tuyến tính "trọng lượng". Giải pháp có thể:

<?xml version="1.0" encoding="utf-8"?> 
<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:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> 
      <TextView android:id="@+id/SearchTextView" 
       android:text="Search" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

      <EditText 
       android:text="@+id/SearchEditText" 
       android:id="@+id/SearchEditText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:singleLine="true"> 
      </EditText> 

     </LinearLayout> 

     <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="0dip" android:layout_weight="1"/> 
      <LinearLayout android:id="@+id/Tab1ListLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 
      </LinearLayout>  

      <Button android:id="@+id/tab2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Tab 2 button"/> 
    </LinearLayout> 
</TabHost> 
+0

OK cảm ơn sự giúp đỡ của bạn. –

+0

Khá hiển nhiên từ Trình xem thứ bậc những gì đã sai - Tôi không biết về công cụ đó. –

Các vấn đề liên quan