fix: padding 自定适配
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -29,7 +29,7 @@
|
|||||||
</value>
|
</value>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
package com.dalimao.corelibrary;
|
package com.dalimao.corelibrary;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.content.res.TypedArray;
|
import android.content.res.TypedArray;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
@@ -18,6 +19,7 @@ import android.text.InputType;
|
|||||||
import android.text.TextWatcher;
|
import android.text.TextWatcher;
|
||||||
import android.text.method.PasswordTransformationMethod;
|
import android.text.method.PasswordTransformationMethod;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.util.DisplayMetrics;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
@@ -234,31 +236,58 @@ public class VerificationCodeInput extends ViewGroup {
|
|||||||
return new LinearLayout.LayoutParams(getContext(), attrs);
|
return new LinearLayout.LayoutParams(getContext(), attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||||
Log.d(getClass().getName(), "onMeasure");
|
int parentWidth = getLayoutParams().width;
|
||||||
int count = getChildCount();
|
if (parentWidth == LayoutParams.MATCH_PARENT) {
|
||||||
|
parentWidth = getScreenWidth();
|
||||||
|
}
|
||||||
|
Log.d(getClass().getName(), "onMeasure width " + parentWidth);
|
||||||
|
|
||||||
|
int count = getChildCount();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
View child = getChildAt(i);
|
View child = getChildAt(i);
|
||||||
this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
|
this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
|
||||||
}
|
}
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
View child = getChildAt(0);
|
View child = getChildAt(0);
|
||||||
int cHeight = child.getMeasuredHeight();
|
|
||||||
int cWidth = child.getMeasuredWidth();
|
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 maxH = cHeight + 2 * childVPadding;
|
||||||
int maxW = (cWidth + childHPadding) * box + childHPadding;
|
int maxW = (cWidth) * count + childHPadding * (count + 1);
|
||||||
setMeasuredDimension(resolveSize(maxW, widthMeasureSpec),
|
setMeasuredDimension(resolveSize(maxW, widthMeasureSpec),
|
||||||
resolveSize(maxH, heightMeasureSpec));
|
resolveSize(maxH, heightMeasureSpec));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getScreenWidth() {
|
||||||
|
|
||||||
|
Resources resources = this.getResources();
|
||||||
|
DisplayMetrics dm = resources.getDisplayMetrics();
|
||||||
|
|
||||||
|
return dm.widthPixels;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
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();
|
int childCount = getChildCount();
|
||||||
|
|
||||||
for (int i = 0; i < childCount; i++) {
|
for (int i = 0; i < childCount; i++) {
|
||||||
@@ -267,7 +296,7 @@ public class VerificationCodeInput extends ViewGroup {
|
|||||||
child.setVisibility(View.VISIBLE);
|
child.setVisibility(View.VISIBLE);
|
||||||
int cWidth = child.getMeasuredWidth();
|
int cWidth = child.getMeasuredWidth();
|
||||||
int cHeight = child.getMeasuredHeight();
|
int cHeight = child.getMeasuredHeight();
|
||||||
int cl = (i) * (cWidth + childHPadding);
|
int cl = childHPadding + (i) * (cWidth + childHPadding);
|
||||||
int cr = cl + cWidth;
|
int cr = cl + cWidth;
|
||||||
int ct = childVPadding;
|
int ct = childVPadding;
|
||||||
int cb = ct + cHeight;
|
int cb = ct + cHeight;
|
||||||
|
|||||||
@@ -26,6 +26,6 @@ dependencies {
|
|||||||
})
|
})
|
||||||
compile 'com.android.support:appcompat-v7:24.2.1'
|
compile 'com.android.support:appcompat-v7:24.2.1'
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
//compile 'com.github.liuguangli:VerificationCodeInput:1.3'
|
//compile 'com.github.liuguangli:VerificationCodeInput:1.5'
|
||||||
compile project(':corelibrary');
|
compile project(':corelibrary');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,87 +10,98 @@
|
|||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
tools:context="com.dalimao.verificationcodeinput.MainActivity">
|
tools:context="com.dalimao.verificationcodeinput.MainActivity">
|
||||||
|
|
||||||
<TextView
|
<ScrollView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content">
|
||||||
android:text="系统样式:"
|
<LinearLayout
|
||||||
/>
|
android:layout_width="match_parent"
|
||||||
<com.dalimao.corelibrary.VerificationCodeInput
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
ver:box="4"
|
android:orientation="vertical">
|
||||||
ver:child_h_padding="5dp"
|
<TextView
|
||||||
android:layout_centerInParent="true"
|
android:layout_width="800px"
|
||||||
android:id="@+id/verificationCodeInput"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="16dp"
|
android:background="@color/colorAccent"
|
||||||
/>
|
android:text="系统样式:"
|
||||||
<TextView
|
/>
|
||||||
android:layout_width="wrap_content"
|
<com.dalimao.corelibrary.VerificationCodeInput
|
||||||
android:layout_height="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:text="自动定义样式:"
|
android:layout_height="wrap_content"
|
||||||
android:padding="12dp"
|
ver:box="4"
|
||||||
/>
|
ver:child_h_padding="5dp"
|
||||||
<com.dalimao.corelibrary.VerificationCodeInput
|
android:layout_centerInParent="true"
|
||||||
android:layout_width="wrap_content"
|
android:id="@+id/verificationCodeInput"
|
||||||
android:layout_height="wrap_content"
|
android:layout_marginBottom="16dp"
|
||||||
ver:box="4"
|
/>
|
||||||
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
|
<TextView
|
||||||
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
|
android:layout_width="wrap_content"
|
||||||
ver:child_h_padding="5dp"
|
android:layout_height="wrap_content"
|
||||||
android:layout_centerInParent="true"
|
android:text="自动定义样式:"
|
||||||
android:layout_marginBottom="16dp"
|
android:padding="12dp"
|
||||||
/>
|
/>
|
||||||
<TextView
|
<com.dalimao.corelibrary.VerificationCodeInput
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="800px"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="定义个数:"
|
android:background="@color/colorAccent"
|
||||||
android:padding="12dp"
|
ver:box="4"
|
||||||
/>
|
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
|
||||||
<com.dalimao.corelibrary.VerificationCodeInput
|
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
|
||||||
android:layout_width="wrap_content"
|
ver:child_h_padding="5dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_centerInParent="true"
|
||||||
ver:box="5"
|
android:layout_marginBottom="16dp"
|
||||||
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
|
/>
|
||||||
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
|
<TextView
|
||||||
ver:child_h_padding="5dp"
|
android:layout_width="wrap_content"
|
||||||
android:layout_centerInParent="true"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="16dp"
|
android:text="定义个数:"
|
||||||
/>
|
android:padding="12dp"
|
||||||
<TextView
|
/>
|
||||||
android:layout_width="wrap_content"
|
<com.dalimao.corelibrary.VerificationCodeInput
|
||||||
android:layout_height="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:text="定义密码类型:"
|
android:layout_height="wrap_content"
|
||||||
android:padding="12dp"
|
ver:box="5"
|
||||||
/>
|
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
|
||||||
<com.dalimao.corelibrary.VerificationCodeInput
|
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
|
||||||
android:layout_width="wrap_content"
|
ver:child_h_padding="5dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_centerInParent="true"
|
||||||
ver:box="5"
|
android:layout_marginBottom="16dp"
|
||||||
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
|
/>
|
||||||
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
|
<TextView
|
||||||
ver:child_h_padding="5dp"
|
android:layout_width="wrap_content"
|
||||||
ver:inputType="password"
|
android:layout_height="wrap_content"
|
||||||
android:layout_centerInParent="true"
|
android:text="定义密码类型:"
|
||||||
android:layout_marginBottom="16dp"
|
android:padding="12dp"
|
||||||
/>
|
/>
|
||||||
|
<com.dalimao.corelibrary.VerificationCodeInput
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
ver:box="5"
|
||||||
|
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
|
||||||
|
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
|
||||||
|
ver:child_h_padding="5dp"
|
||||||
|
ver:inputType="password"
|
||||||
|
android:layout_centerInParent="true"
|
||||||
|
android:layout_marginBottom="16dp"
|
||||||
|
/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="定义大小:"
|
android:text="定义大小:"
|
||||||
android:padding="12dp"
|
android:padding="12dp"
|
||||||
/>
|
/>
|
||||||
<com.dalimao.corelibrary.VerificationCodeInput
|
<com.dalimao.corelibrary.VerificationCodeInput
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
ver:box="5"
|
ver:box="5"
|
||||||
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
|
ver:box_bg_normal="@drawable/verification_edit_bg_normal"
|
||||||
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
|
ver:box_bg_focus="@drawable/verification_edit_bg_focus"
|
||||||
ver:child_h_padding="5dp"
|
ver:child_h_padding="5dp"
|
||||||
ver:child_width="60dp"
|
ver:child_width="60dp"
|
||||||
ver:child_height="40dp"
|
ver:child_height="40dp"
|
||||||
ver:inputType="password"
|
ver:inputType="password"
|
||||||
android:layout_centerInParent="true"
|
android:layout_centerInParent="true"
|
||||||
android:layout_marginBottom="16dp"
|
android:layout_marginBottom="16dp"
|
||||||
/>
|
/>
|
||||||
|
</LinearLayout>
|
||||||
|
</ScrollView>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|||||||
Reference in New Issue
Block a user