问题描述
我的屏幕被分成两个布局。
左侧(clients_list_layout)是确定的,但我有一个问题右侧(detail_layout)。它包括两个TextViews的。我想第一个电视来包装它的内容,并采取不超过30父布局(detail_layout)。
My screen is divided into two layouts.Left side(clients_list_layout) is OK, but I have one problem with right side(detail_layout). It consists of two TextViews. I want first TV to wrap it's content and to take not more then 30 percent of parent layout(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%每次。
What should I do to make "client_comments" take from 0 to 30 percent of parent layout, not 30 percent everytime.
推荐答案
我有类似的问题,最后用自定义布局的基础上,LinearLayout中结束。我重写onMeasure方法,此布局来设置它的子视图宽度无论是包裹内容或宽度成正比,重量值:
I have similar issue and finally end up with custom layout, based on LinearLayout. I override onMeasure method for this layout to set it's child views width either to wrap content or width proportional to weight value:
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);
}
}
}
}
用这个布局示例活动:
Sample activity with this layout:
<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>
这篇关于TextView的高度WRAP_CONTENT但父母不超过30%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!