TextView高度wrap_content但不超过父级的30%

时间:2014-04-13 14:46:54

标签: android android-layout textview android-linearlayout

我的屏幕分为两种布局。 左侧(clients_list_layout)没问题,但右侧有一个问题(detail_layout)。它由两个TextView组成。我想要第一个电视来包装它的内容,并且不要超过30%的父布局(detail_layout)。

现在我有下一个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%。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分: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>