package com.study.myapplication;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import androidx.annotation.Nullable;
public class TextLinearLayout extends RelativeLayout {
private static final String TAG = TextLinearLayout.class.getSimpleName();
private boolean isMeasure = false;
public TextLinearLayout(Context context) {
super(context);
}
public TextLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TextLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int layoutLength = this.getMeasuredWidth();
View leftView = getChildAt(0);
View rightView = getChildAt(1);
int left = leftView.getMeasuredWidth();
int right = rightView.getMeasuredWidth();
int actualLength = left + right;
int paddingLength = this.getPaddingLeft() + this.getPaddingRight() + 32;
Log.e(TAG, "layout:" + layoutLength + " left:" + left + " right:" + right + " padding:" + paddingLength
+ " all:" + (left + right + paddingLength) + " isChao:" + isMeasure);
if (actualLength < (layoutLength - paddingLength)) {
Log.e(TAG, "没有超。。。");
return;
} else {
if (isMeasure == true) {
return;
}
int leftWith = (int) ((layoutLength) * (1.0 * left / actualLength));
int rightWith = (int) ((layoutLength) * (1.0 * right / actualLength));
Log.e(TAG, "left " + leftWith + " right" + rightWith
+" paddlf:"+leftView.getPaddingRight()+" paddrih:"+rightView.getPaddingLeft());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(leftWith-24
, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
leftView.setLayoutParams(lp);
RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams(rightWith
, RelativeLayout.LayoutParams.WRAP_CONTENT);
ll.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
rightView.setLayoutParams(ll);
isMeasure = true;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
Log.e("LAYOUT", "layout...");
}
}
<com.study.myapplication.TextLinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingStart="12dp"
android:paddingLeft="12dp"
android:paddingEnd="12dp"
android:paddingRight="12dp"
>
<TextView
android:id="@+id/tv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:paddingRight="8dp"
android:layout_marginBottom="10dp"
android:text="custom service"
android:textSize="17sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="end|center_vertical">
<TextView
android:id="@+id/tv_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end|right"
android:text="2355"
android:textSize="17sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/arrow_back_white" />
</LinearLayout>
</com.study.myapplication.TextLinearLayout>
网友评论