1.定义属性:
<declare-styleable name="AutoFitLinearLayout">
<attr name="auto_fit_view" format="reference"/>
</declare-styleable>
2.自定布局
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import com.kidswant.component.R;
/**
* @author bruce.zhang
* @date 2017/11/23 11:07
* @description LinearLayout的width是wrap_content,随着内容的变长LinearLayout也跟着变长,当LinearLayout
* 达到父布局边际时,如果内容继续增加,LinearLayout不会变宽(这是重点,该自定义策略就是根据LinearLayout不会超过
* 父布局约束的宽度来处理的,RelativeLayout就不行,会随着内容变长而变长,即使超过父布局约束)。
* 此控件可以控制可变部分的宽度,不让整体内容超过LinearLayout
* <p>
* modification history:
*/
public class AutoFitLinearLayout extends LinearLayout {
private int autoFitViewId;
private View autoFitView;
public AutoFitLinearLayout(Context context) {
this(context, null);
}
public AutoFitLinearLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoFitLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AutoFitLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AutoFitLinearLayout);
int autoFitViewId = ta.getResourceId(R.styleable.AutoFitLinearLayout_auto_fit_view, 0);
ta.recycle();
if(autoFitViewId <= 0) {
throw new IllegalArgumentException("AutoFitLinearLayout autoFitViewId not valid");
}
this.autoFitViewId = autoFitViewId;
}
private View getAutoFitView()
{
if (autoFitView == null) {
autoFitView = findViewById(autoFitViewId);
}
return autoFitView;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
View autoFitChild = getAutoFitView();
int autoFitWidth = 0;
int otherContentWidth = 0;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
if (child.getLayoutParams() instanceof MarginLayoutParams) {
otherContentWidth += ((MarginLayoutParams)child.getLayoutParams()).leftMargin +
((MarginLayoutParams)child.getLayoutParams()).rightMargin;
}
if(child == autoFitChild) {
autoFitWidth = child.getMeasuredWidth();
} else {
otherContentWidth += child.getMeasuredWidth();
}
}
/*Log.i("aaaaaaaa", "1----: " + (getMeasuredWidth() - getPaddingLeft() - getPaddingRight())
+ "---" + otherContentWidth + "---" + autoFitWidth);*/
if (otherContentWidth + autoFitWidth > getMeasuredWidth() - getPaddingLeft() - getPaddingRight()) {
autoFitWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - otherContentWidth;
LayoutParams params = (LayoutParams) autoFitChild.getLayoutParams();
params.width = autoFitWidth;
measureChild(autoFitChild, MeasureSpec.makeMeasureSpec(autoFitWidth, MeasureSpec.EXACTLY),
heightMeasureSpec);
}
}
}
3.使用
<?xml version="1.0" encoding="utf-8"?>
<com.kidswant.component.view.AutoFitLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/chat_ll_audio_layout"
tools:background="@color/kidim_FF397E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
app:auto_fit_view="@+id/chat_tv_duration_space"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:focusable="false" >
<ImageView
android:id="@+id/chat_img_audio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="15.0dip"
android:minWidth="15.0dip"
android:scaleType="centerInside"
android:src="@drawable/chat_audio_left_03" />
<TextView
android:id="@+id/chat_tv_duration"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:minWidth="30dp"
android:gravity="left"
tools:text="180'"
android:textSize="14sp"
android:textColor="@android:color/white" />
<!--可变部分 -->
<android.support.v4.widget.Space
android:id="@+id/chat_tv_duration_space"
android:layout_width="100dp"
android:layout_height="1px" />
<ProgressBar
android:id="@+id/chat_audio_progressbar"
style="@style/chat_loading"
android:layout_width="20dp"
android:layout_height="20dp"
tools:visibility="visible"
android:visibility="invisible" />
</com.kidswant.component.view.AutoFitLinearLayout>