Skip to content

Commit

Permalink
ios alert 的msg的gravity自适应
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Jan 11, 2023
1 parent 2ac948e commit a0841b8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/com/hss01248/dialogutildemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions dialog/src/main/java/com/hss01248/dialog/ScreenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
* 通过反射,获取包含虚拟键的整体屏幕高度
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions dialog/src/main/res/layout/dialog_ios_alert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

<EditText
Expand Down

0 comments on commit a0841b8

Please sign in to comment.