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.
*/
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>
Solution 2:
try this
<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>
Solution 3:
Try this..
<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>
Post a Comment for "TextView Height Wrap_content But Not More Than 30 Percent Of Parent"