Textview Height Wrap_content But Not More Than 30 Percent Of Parent
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
Solution 1:
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.
*/publicclassEvenLayoutextendsLinearLayout {
privatestaticbooleanDEBUG=false;
privatestaticfinalStringTAG="EvenLayout";
publicstaticvoidsetDebug(boolean debug){
DEBUG = debug;
}
publicEvenLayout(Context context) {
super(context);
}
publicEvenLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicEvenLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
finalintwidthMode= MeasureSpec.getMode(widthMeasureSpec);
finalintheightMode= MeasureSpec.getMode(heightMeasureSpec);
finalintwidth= MeasureSpec.getSize(widthMeasureSpec);
finalintheight= MeasureSpec.getSize(heightMeasureSpec);
finalintcount= getChildCount();
floattotalWeight=0;
for (inti=0; i < count; ++i) {
finalViewchild= getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
finalLayoutParamslp= (LayoutParams)
child.getLayoutParams();
totalWeight += lp.weight;
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
}
intaccWidth=0;
for (inti=0; i < count; ++i) {
finalViewchild= getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
if (i < count - 1) {
finalLayoutParamslp= (LayoutParams)
child.getLayoutParams();
if (lp.weight > 0) {
intmaxChildWidth= (int) (lp.weight / totalWeight * width);
if (maxChildWidth < child.getMeasuredWidth()) {
child.measure(
MeasureSpec.makeMeasureSpec(maxChildWidth, MeasureSpec.EXACTLY),
heightMeasureSpec);
}
}
accWidth += child.getMeasuredWidth();
} else {
intremainingWidth= width - accWidth;
child.measure(
MeasureSpec.makeMeasureSpec(remainingWidth, MeasureSpec.EXACTLY),
heightMeasureSpec);
}
}
}
}
Sample activity with this layout:
<FrameLayoutxmlns: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.EvenLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid: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"/><TextViewandroid: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>
Solution 2:
try this
<LinearLayoutandroid:id="@+id/detail_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="50"android:orientation="vertical"android:weightSum="100" ><TextViewandroid: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" /><TextViewandroid: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>
Solution 3:
Try this..
<LinearLayoutandroid:id="@+id/detail_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="50"android:orientation="vertical"android:weightSum="100" ><TextViewandroid: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" /><TextViewandroid: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>
Post a Comment for "Textview Height Wrap_content But Not More Than 30 Percent Of Parent"