2014-04-13 54 views
1

我的屏幕分爲兩種佈局。 左側(clients_list_layout)正常,但右側(detail_layout)有一個問題。它由兩個TextView組成。我希望第一臺電視機能夠包裝它的內容,並且不超過30%的家長佈局(detail_layout)。TextView高度wrap_content但不超過家長的30%

現在我旁邊的xml:

<?xml version="1.0" encoding="utf-8"?> 
<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="match_parent" 
android:baselineAligned="false" 
android:orientation="horizontal" 
android:weightSum="100" > 

<LinearLayout 
    android:id="@+id/clients_list_layout" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="50" 
    android:descendantFocusability="beforeDescendants" 
    android:focusableInTouchMode="true" 
    android:orientation="vertical" 
    android:weightSum="100" > 

    ....... 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/detail_layout" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="50" 
    android:orientation="vertical" 
    android:weightSum="100" > 

    <TextView 
     android:id="@+id/client_comments" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="30" 
     android:padding="5dp" 
     android:scrollbarAlwaysDrawVerticalTrack="true" 
     android:scrollbars="vertical" 
     android:text="" 
     android:textSize="20sp" 
     tools:ignore="NestedWeights" /> 

    <TextView 
     android:id="@+id/client_debt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:textSize="20sp" 
     android:text="" /> 

</LinearLayout> 

我應該怎麼做才能讓 「client_comments」 從0%到30%的父佈局,而不是30%,每次服用。

回答

0

試試這個

<LinearLayout 
    android:id="@+id/detail_layout" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="50" 
    android:orientation="vertical" 
    android:weightSum="100" > 

    <TextView 
     android:id="@+id/client_comments" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="70" 
     android:padding="5dp" 
     android:scrollbarAlwaysDrawVerticalTrack="true" 
     android:scrollbars="vertical" 
     android:text="" 
     android:textSize="20sp" 
     tools:ignore="NestedWeights" /> 

    <TextView 
     android:id="@+id/client_debt" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="30" 
     android:padding="2dp" 
     android:textSize="20sp" 
     android:text="" /> 

</LinearLayout> 
+0

沒有運氣。即使只有一個註釋字,「client_comments」也會佔用它所包含的文本大小的70%的父高度。 –

0

試試這個..

<LinearLayout 
    android:id="@+id/detail_layout" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="50" 
    android:orientation="vertical" 
    android:weightSum="100" > 

    <TextView 
     android:id="@+id/client_comments" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="30" 
     android:padding="5dp" 
     android:scrollbarAlwaysDrawVerticalTrack="true" 
     android:scrollbars="vertical" 
     android:text="" 
     android:textSize="20sp" 
     tools:ignore="NestedWeights" /> 

    <TextView 
     android:id="@+id/client_debt" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="70" 
     android:padding="2dp" 
     android:textSize="20sp" 
     android:text="" /> 

</LinearLayout> 
+0

沒有運氣。即使只有一個評論字,「client_comments」也會將其包含的30%的父高度與其包含的文本大小進行比較 –

0

我有類似的問題,最後用自定義佈局的基礎上,LinearLayout中結束。我重寫onMeasure方法此佈局來設置它的子視圖寬度要麼包裹內容或寬度成正比,權重值:

package com.snaprix.androidfrequentlyused.views; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.LinearLayout; 

/** 
* Created by vladimirryabchikov on 8/26/14. 
*/ 
public class EvenLayout extends LinearLayout { 
    private static boolean DEBUG = false; 
    private static final String TAG = "EvenLayout"; 

    public static void setDebug(boolean debug){ 
     DEBUG = debug; 
    } 

    public EvenLayout(Context context) { 
     super(context); 
    } 

    public EvenLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public EvenLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
     final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 

     final int width = MeasureSpec.getSize(widthMeasureSpec); 
     final int height = MeasureSpec.getSize(heightMeasureSpec); 

     final int count = getChildCount(); 
     float totalWeight = 0; 
     for (int i = 0; i < count; ++i) { 
      final View child = getChildAt(i); 

      if (child.getVisibility() == GONE) { 
       continue; 
      } 

      final LayoutParams lp = (LayoutParams) 
        child.getLayoutParams(); 

      totalWeight += lp.weight; 

      measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); 
     } 

     int accWidth = 0; 
     for (int i = 0; i < count; ++i) { 
      final View child = getChildAt(i); 

      if (child.getVisibility() == GONE) { 
       continue; 
      } 

      if (i < count - 1) { 
       final LayoutParams lp = (LayoutParams) 
         child.getLayoutParams(); 
       if (lp.weight > 0) { 
        int maxChildWidth = (int) (lp.weight/totalWeight * width); 
        if (maxChildWidth < child.getMeasuredWidth()) { 
         child.measure(
           MeasureSpec.makeMeasureSpec(maxChildWidth, MeasureSpec.EXACTLY), 
           heightMeasureSpec); 
        } 
       } 
       accWidth += child.getMeasuredWidth(); 
      } else { 
       int remainingWidth = width - accWidth; 
       child.measure(
         MeasureSpec.makeMeasureSpec(remainingWidth, MeasureSpec.EXACTLY), 
         heightMeasureSpec); 
      } 
     } 
    } 
} 

樣活性用這個佈局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.snaprix.androidfrequentlyused.views.EvenLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:text="Framer Studio 1.6 with Revamped Sketch Support" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="30" 
      android:singleLine="true"/> 

     <TextView 
      android:text="In close collaboration with the Bohemian Coding team, we’re happy to announce much improved Sketch support in Framer Studio" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="70" 
      android:singleLine="true"/> 
    </com.snaprix.androidfrequentlyused.views.EvenLayout> 
</FrameLayout>