1

單擊TextView時,屏幕上顯示DialogFragment。該DialogFragment擁有自己的XML佈局文件,其底部爲ListViewEditTextButtonAndroid DialogFragment不包含WRAP_CONTENT

的問題是,當ListView只有幾個項目,該DialogFragment仍然佔據了大部分屏幕(即高度遠遠大於它應該是。

這裏是我的意思截圖(黑色是ListView一個項目):

enter image description here

正如你可以看到有的ListView以上空格時,我想Dialog是隻有它需要的大小豐盈

這裏是DialogFragent XML:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/spacing_small" 
    tools:context="com.cohenadair.anglerslog.fragments.ManageFragment"> 

    <LinearLayout 
     android:id="@+id/add_item_layout" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true"> 

     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/new_item_edit" 
      android:layout_weight="1" 
      android:hint="@string/hint_new_item"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_add" 
      android:id="@+id/add_button" 
      android:layout_margin="0dp"/> 

    </LinearLayout> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/add_item_layout" 
     android:id="@+id/content_list_view" 
     android:background="@android:color/black"> 

    </ListView> 

</RelativeLayout> 

而且onCreateView

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_manage_primitive, container, false); 

    int primitiveId = getArguments().getInt(ARG_PRIMITIVE_ID); 
    PrimitiveFragmentInfo info = FragmentUtils.primitiveInfo(getActivity(), primitiveId); 

    if (info != null) { 
     initViews(view, info); 
     getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 
    } 

    return view; 
} 

謝謝!

回答

6

我通過從RelativeLayout轉換並使用LinearLayoutlayout_weight屬性解決了問題。

這裏的新的XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/spacing_small" 
    android:orientation="vertical" 
    tools:context="com.cohenadair.anglerslog.fragments.ManageFragment"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:id="@+id/content_list_view"> 

    </ListView> 

    <LinearLayout 
     android:id="@+id/add_item_layout" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/new_item_edit" 
      android:layout_weight="1" 
      android:hint="@string/hint_new_item"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_add" 
      android:id="@+id/add_button"/> 

    </LinearLayout> 

</LinearLayout> 
+0

感謝。有用! – iroiroys

+0

但是,RelativeLayout仍然存在問題。 –

+0

@DevetiPutnik正確。我永遠無法弄清楚爲什麼'RelativeLayout'不能工作,或者使用'RelativeLayout'的適當解決方案。這個答案顯然只是一種解決方法。 – cohenadair