2011-10-28 35 views
36

Xin lỗi về việc xuất mã lớn, nhưng tôi thực sự bị mất.Phân đoạn Android không tôn trọng match_parent là chiều cao

MyActivity.java onCreate:

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_singlepane_empty); 
mFragment = new PlacesFragment(); 
getSupportFragmentManager().beginTransaction() 
        .add(R.id.root_container, mFragment) 
        .commit(); 

PlacesFragment.java onCreateView:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null); 
return mRootView; 

Ghi chú: mRootView là một ViewGroup toàn cầu, không có vấn đề về điều đó, tôi tin. PlacesFragment là một ListFragment.

Layouts:

activity_singlepane_empty.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root_container" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#00f"> 
    <include layout="@layout/actionbar"/> 

    <!-- FRAGMENTS COME HERE! See match_parent above --> 

</LinearLayout> 

list_content.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/listContainer" 
     android:background="#990" 
     > 

     <ListView android:id="@android:id/list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:drawSelectorOnTop="false" /> 

     <TextView android:id="@id/android:empty" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:text="@string/no_data_suggest_connection"/> 

</FrameLayout> 

Vấn đề: như bạn có thể thấy, hành vi mong đợi sẽ có những sản phẩm nào TextView ở trên xuất hiện ở giữa màn hình. Trên bản xem trước thiết kế trong Eclipse, nó là OK. Chỉ khi được thêm vào root_view dưới dạng một đoạn, FrameLayout sẽ không lấp đầy toàn bộ màn hình.

root_container màu xanh lam và FrameLayout có màu vàng, xem bên dưới để biết mục đích gỡ lỗi. Không phải khung màu vàng sẽ lấp đầy toàn bộ màn hình?!?!?!?

enter image description here

+0

Nếu bạn sử dụng chế độ xem ScrollView: http://stackoverflow.com/questions/10211338/view-inside-scrollview-doesnt-take-all-place –

Trả lời

69

tôi đã cùng một vấn đề và nghĩ rằng nó sẽ xảy ra khi bạn thổi phồng bố trí trong onCreateView của Fragment với null, như bạn đã làm ở đây:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null); 

Thay vào đó bạn phải làm điều này:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content,container, false); 

đâu contai ner là Viewgroup. Ít nhất, điều đó giải quyết vấn đề cho tôi.

+2

http://developer.android.com/reference/android/view/LayoutInflater.html Chế độ xem tùy chọn làm cha mẹ của hệ thống phân cấp được tạo (nếu attachToRoot là đúng) hoặc đơn giản chỉ là đối tượng cung cấp bộ LayoutParams các giá trị cho gốc của hệ thống phân cấp được trả về (nếu attachToRoot là sai.) Đã chấp nhận câu trả lời. Cảm ơn bạn. – davidcesarino

+1

cũng xem http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater – Stevie

3

Đối với một số lý do FrameLayout không được bố trí của nó từ XML.

tôi cần phải đặt nó cũng trong mã (Fragment của onCreateView):

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null); 
FrameLayout fl = (FrameLayout) mRootView.findViewById(R.id.listContainer); 
fl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
return mRootView; 
+0

Đây là câu trả lời cũ. Kể từ đó, tôi chuyển dấu chấp nhận sang câu trả lời của Norman. Tất nhiên câu trả lời này cung cấp sự linh hoạt hơn, nhưng tôi rất vui khi được chấp nhận vì câu hỏi này đã bảo đảm rõ ràng một giải pháp cho việc sử dụng dễ dàng. – davidcesarino

0

Con đường tôi đã làm điều đó là tạo ra một hình ảnh trong suốt trong mã xml, làm cho các mảnh một cách bố trí tương đối và làm cho layout_alignParentBottom="true" trong ImageView, cách này gậy ảnh để phía dưới và làm cho các mảnh điền phụ huynh

Mã đây:

temp_transparent.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:thickness="0dp" 
    android:shape="rectangle"> 
<gradient 
    android:startColor="#00000000" 
    android:endColor="#00000000" 
    android:type="linear" 
    android:angle="270"/> 

fragment_m y_invites.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:id="@+id/my_invites_fragment" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#ffffff"> 


<ListView 
    android:id="@+id/list_invites" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:divider="@color/list_divider" 
    android:dividerHeight="1dp" 
    android:layout_alignParentTop="true" > 
</ListView> 

<ImageView 
    android:id="@+id/transparent_image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_below="@id/list_invites" 
    android:src="@drawable/temp_transparent" /> 

4
<android.support.v4.widget.NestedScrollView 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    android:fillViewport="true" 
    > 

    <include layout="@layout/screen_main_fragment" /> 

</android.support.v4.widget.NestedScrollView> 

tôi đã phải thay đổi layout_height = "wrap_content" để "match_parent" để làm cho nó hoạt động.

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