Here is an oversimplified example, that does not work:
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/red"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Label1 \n Line2 \n Line3"
android:background="@color/green"
/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/blue"
android:layout_alignParentRight="true"
android:text="Label2 with\n two lines"/>
</android.support.percent.PercentRelativeLayout>
What will be the simplest way to solve this problem?
As a workaround I did the following, please comment if this is ok to do (changing the children height in onMeasure)
public class PercentageRelativeLayout extends PercentRelativeLayout
{
public PercentageRelativeLayout(Context context) {
super(context);
}
public PercentageRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PercentageRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
ArrayList<ViewGroup.LayoutParams> modifiedLayouts = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getLayoutParams().height == LayoutParams.MATCH_PARENT) {
child.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
modifiedLayouts.add(child.getLayoutParams());
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(modifiedLayouts.size() > 0) {
int heightHint = getMeasuredHeight();
if(heightHint > 0) {
getLayoutParams().height = heightHint;
}
for (ViewGroup.LayoutParams params : modifiedLayouts)
params.height = LayoutParams.MATCH_PARENT;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.