diff --git a/app/src/main/java/com/hss01248/dialogutildemo/MainActivity.java b/app/src/main/java/com/hss01248/dialogutildemo/MainActivity.java index 4dd1fe0..da7d987 100644 --- a/app/src/main/java/com/hss01248/dialogutildemo/MainActivity.java +++ b/app/src/main/java/com/hss01248/dialogutildemo/MainActivity.java @@ -404,6 +404,18 @@ public void onThird() { } + }).show(); + StyledDialog.buildIosAlert( "titlexxxxxxxxxx\nxxxxxxxxxxxt\nitlexxxxxxxxx\nxxxxxxxxxxx", + "titlexxxxxxxxxx\nxxxxxxxxxxxt\nitlexxxxxxxxx\nxxxxxxxxxxx", + new MyDialogListener() { + @Override + public void onFirst() { + showToast("onFirst"); + } + @Override + public void onSecond() { + + } }).show(); break; case R.id.btn_ios_alert_vertical: diff --git a/dialog/src/main/java/com/hss01248/dialog/ScreenUtil.java b/dialog/src/main/java/com/hss01248/dialog/ScreenUtil.java index ef7b473..e0783f9 100644 --- a/dialog/src/main/java/com/hss01248/dialog/ScreenUtil.java +++ b/dialog/src/main/java/com/hss01248/dialog/ScreenUtil.java @@ -21,6 +21,10 @@ public static int getScreenHeight() { int height = getWindowManager().getDefaultDisplay().getHeight(); return height; } + public static int getScreenWidth() { + int height = getWindowManager().getDefaultDisplay().getWidth(); + return height; + } /** * 通过反射,获取包含虚拟键的整体屏幕高度 diff --git a/dialog/src/main/java/com/hss01248/dialog/ios/IosAlertDialogHolder.java b/dialog/src/main/java/com/hss01248/dialog/ios/IosAlertDialogHolder.java index a14a1f0..d9721aa 100644 --- a/dialog/src/main/java/com/hss01248/dialog/ios/IosAlertDialogHolder.java +++ b/dialog/src/main/java/com/hss01248/dialog/ios/IosAlertDialogHolder.java @@ -6,6 +6,7 @@ import android.text.TextUtils; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; @@ -17,6 +18,8 @@ import android.widget.TextView; import com.hss01248.dialog.R; +import com.hss01248.dialog.ScreenUtil; +import com.hss01248.dialog.StyledDialog; import com.hss01248.dialog.Tool; import com.hss01248.dialog.adapter.SuperLvHolder; import com.hss01248.dialog.config.ConfigBean; @@ -209,6 +212,16 @@ public void onClick(View view) { }); } + /** + * 策略: textview 使用wrap_content, + * 如果其宽度没有充满,那么将其和其内文字居中显示 + * 否则,居左 + * json类型的不调整其gravity + * 测试: 多行 短文字 + * 单行 短文字 + * 单行 长文字 + * @param bean + */ private void setMsgStyleAndTxt(ConfigBean bean) { if (TextUtils.isEmpty(bean.msg)) { tvMsg.setVisibility(View.GONE); @@ -219,17 +232,27 @@ private void setMsgStyleAndTxt(ConfigBean bean) { tvMsg.setTextColor(Tool.getColor(tvMsg.getContext(),bean.msgTxtColor)); tvMsg.setTextSize(bean.msgTxtSize); - tvMsg.measure(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); + //json类型的不调整其gravity + if(bean.msg.toString().startsWith("{") && bean.msg.toString().endsWith("}")){ + return; + } + if(bean.msg.toString().startsWith("[") && bean.msg.toString().endsWith("]")){ + return; + } tvMsg.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int lineCount = tvMsg.getLineCount(); if(lineCount>0){ tvMsg.getViewTreeObserver().removeOnGlobalLayoutListener(this); - if (lineCount ==1){ + + Log.w("check","tvMsg.getMeasuredWidth()-"+tvMsg.getMeasuredWidth()+ + "-llContainerContent.getMeasuredWidth()-"+llContainerContent.getMeasuredWidth()); + if(tvMsg.getMeasuredWidth()*1.4 < llContainerContent.getMeasuredWidth() || lineCount == 1){ tvMsg.setGravity(Gravity.CENTER); - }else { - tvMsg.setGravity(Gravity.LEFT); + LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tvMsg.getLayoutParams(); + layoutParams.gravity = Gravity.CENTER; + tvMsg.setLayoutParams(layoutParams); } } } diff --git a/dialog/src/main/res/layout/dialog_ios_alert.xml b/dialog/src/main/res/layout/dialog_ios_alert.xml index 9c0932c..cb03ef4 100644 --- a/dialog/src/main/res/layout/dialog_ios_alert.xml +++ b/dialog/src/main/res/layout/dialog_ios_alert.xml @@ -48,14 +48,14 @@ android:id="@+id/tv_msg" android:textSize="@dimen/dialogutil_msg_txt_size" android:textColor="@color/dialogutil_text_msg_33" - android:gravity="center" + android:gravity="left" android:text="i am msg" android:paddingTop="5dp" android:paddingBottom="15dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:minHeight="60dp" - android:layout_width="match_parent" + android:layout_width="wrap_content" android:layout_height="wrap_content"/>