fix: padding 自定适配

This commit is contained in:
刘光利
2019-04-23 15:35:17 +08:00
parent 152cb523e4
commit 0f2ae98ef3
4 changed files with 129 additions and 89 deletions

2
.idea/misc.xml generated
View File

@@ -29,7 +29,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@@ -9,6 +9,7 @@
package com.dalimao.corelibrary;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
@@ -18,6 +19,7 @@ import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
@@ -234,31 +236,58 @@ public class VerificationCodeInput extends ViewGroup {
return new LinearLayout.LayoutParams(getContext(), attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d(getClass().getName(), "onMeasure");
int count = getChildCount();
int parentWidth = getLayoutParams().width;
if (parentWidth == LayoutParams.MATCH_PARENT) {
parentWidth = getScreenWidth();
}
Log.d(getClass().getName(), "onMeasure width " + parentWidth);
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
if (count > 0) {
View child = getChildAt(0);
int cHeight = child.getMeasuredHeight();
int cWidth = child.getMeasuredWidth();
if (parentWidth != LayoutParams.WRAP_CONTENT) {
// 重新计算padding
childHPadding = (parentWidth - cWidth * count) / (count + 1);
}
int cHeight = child.getMeasuredHeight();
int maxH = cHeight + 2 * childVPadding;
int maxW = (cWidth + childHPadding) * box + childHPadding;
int maxW = (cWidth) * count + childHPadding * (count + 1);
setMeasuredDimension(resolveSize(maxW, widthMeasureSpec),
resolveSize(maxH, heightMeasureSpec));
}
}
private int getScreenWidth() {
Resources resources = this.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.widthPixels;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.d(getClass().getName(), "onLayout");
Log.d(getClass().getName(), "onLayout width = " + getMeasuredWidth());
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
@@ -267,7 +296,7 @@ public class VerificationCodeInput extends ViewGroup {
child.setVisibility(View.VISIBLE);
int cWidth = child.getMeasuredWidth();
int cHeight = child.getMeasuredHeight();
int cl = (i) * (cWidth + childHPadding);
int cl = childHPadding + (i) * (cWidth + childHPadding);
int cr = cl + cWidth;
int ct = childVPadding;
int cb = ct + cHeight;

View File

@@ -26,6 +26,6 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
//compile 'com.github.liuguangli:VerificationCodeInput:1.3'
//compile 'com.github.liuguangli:VerificationCodeInput:1.5'
compile project(':corelibrary');
}

View File

@@ -10,13 +10,21 @@
android:orientation="vertical"
tools:context="com.dalimao.verificationcodeinput.MainActivity">
<TextView
android:layout_width="wrap_content"
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="800px"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="系统样式:"
/>
<com.dalimao.corelibrary.VerificationCodeInput
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ver:box="4"
ver:child_h_padding="5dp"
@@ -31,8 +39,9 @@
android:padding="12dp"
/>
<com.dalimao.corelibrary.VerificationCodeInput
android:layout_width="wrap_content"
android:layout_width="800px"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
ver:box="4"
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
@@ -94,3 +103,5 @@
android:layout_marginBottom="16dp"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>