2016-04-17 78 views
0

工作,我有這基本上是簡單地顯示了交易的一些信息的自定義視圖,這是佈局:的LinearLayout重量不DialogFragment

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:background="#a8d1ff" 
    android:weightSum="8"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:orientation="vertical" 
     android:weightSum="6"> 

     <FrameLayout 
      android:id="@+id/leftPartRowOneRelativeLayout" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3"> 

      <ImageView 
       android:id="@+id/nzNumberImageView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:adjustViewBounds="true" 
       android:scaleType="fitXY" 
       android:src="@drawable/transaction_view_leftupside_nz_test" /> 

      <TextView 
       android:id="@+id/nzNumberTextView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="right" 
       android:text="18" 
       android:textSize="40dp" /> 
     </FrameLayout> 

     <ImageView 
      android:id="@+id/productNumberImageView" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3" 
      android:adjustViewBounds="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/transaction_view_leftdownside_product_test" /> 
    </LinearLayout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/trx_summary_LinearLayout" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="6" 
     android:weightSum="10" 
     android:orientation="vertical"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3" 
      android:textSize="20dp" 
      android:textColor="#FF000000" 
      android:id="@+id/palceHolder0" android:text="testRow0"/> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3" 
      android:textSize="20dp" 
      android:textColor="#FF000000" 
      android:id="@+id/palceHolder1" android:text="testRow1"/> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="4" 
      android:textSize="20dp" 
      android:textColor="#FF000000" 
      android:id="@+id/palceHolder2" android:text="testRow2"/> 
    </LinearLayout> 
</LinearLayout> 

這它是什麼樣子Android Studio中預覽,才滿足我的期望(左和右與比2劃分滿父容器:6):

looks good

以後,這一觀點將在DialogFragment, 顯示並這是onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)DialogFragment

MyView mv = new MyView(getActivity().getBaseContext()); 
mv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
return mv; 

但現在,重量的2:6似乎工作任何更多的彈出Dialog

not good

我使用的是Android 6.

任何人都可以幫忙嗎?

[edit0]: 我想Dialog顯示爲屏幕寬度,但半屏幕的高度,所以我把這個代碼在MyDialogFragment

@Override 
public void onResume() { 
    ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes(); 
    params.width = ViewGroup.LayoutParams.MATCH_PARENT; 
    params.height = this.height; 
    getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 
    super.onResume(); 
} 

this.heightbundle這是screen height/2傳遞英寸

回答

0

請確保您在處理layout_weight時處處使用match_parent(或固定尺寸)。如果您在某個時候使用wrap_content,則佈局會根據其子女調整其大小並忽略其重量值。

對於您的情況,您在爲對話視圖充氣時指定wrap_content。這應該是match_parent代替:

MyView mv = new MyView(getActivity().getBaseContext()); 
mv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
return mv; 

還要確保對話窗口本身有一個固定的寬度或match_parent

+0

我已經將'Match_Parent'設置爲您粘貼的代碼,但是我不會爲'Dialog'設置相同的值,因爲我不希望Dialog窗口占據整個屏幕,我只想使它成爲一半屏幕高度。我做了測試,結果仍然是一樣的。 – Shawn

+0

@Shawn你能設置一個固定的寬度/高度(例如'300dp左右)到視圖嗎?如果解決了重量問題,則可以在運行時爲對話框計算匹配大小,並將其設置爲其寬度/高度。 – Floern

+0

我嘗試過'mv.setLayoutParams(new ViewGroup.LayoutParams(900,300));'如你所說,但仍然一樣 – Shawn