在实际开发中,系统自带的dialog样式太丑,往往都是自定义一个布局,通过LayoutInflater.inflate(XXX)打一个布局渲染进去的。但是这样做也有缺点,比如下面这张图,你布局里面是不好处理的:
首先,看一下效果图:
那么遇到这种情况怎么做呢?下面我来分析一下:
一、右边的那个锁是一个状态选择器,我在drawable下面写一个状态选择器,我取名为radio_button_button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_radio_button_select" android:state_activated="true"></item>
<item android:drawable="@drawable/ic_radio_button_select" android:state_pressed="true"></item>
<item android:drawable="@drawable/ic_radio_button_normal"></item></selector>
二、改写系统的布局,我取名为:select_dialog_singlechoice.xml 具体如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:checkMark="@drawable/radio_button_button_selector"
android:gravity="center_vertical"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:textAppearance="?android:attr/textAppearanceListItemSmall"/></LinearLayout>
三、然后在activity的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击就有弹窗"/></LinearLayout>
四、最后就是mainactivity里面的代码:
/**
* 弹窗的背景色的设置
*/public class MainActivity3 extends AppCompatActivity implements View.OnClickListener{ private CharSequence[] items = new CharSequence[]{"苹果", "西瓜", "香蕉", "葡萄","你想要啥水果自己选。。"};
private Button mButton;
ArrayAdapter<SpannableStringBuilder> adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mButton = (Button) findViewById(R.id.button3);
mButton.setOnClickListener(this);
ArrayList<SpannableStringBuilder> chooseTypeList = new ArrayList<>();
SpannableStringBuilder builder1 = new SpannableStringBuilder();
SpannableString ss1 = new SpannableString("湖北省 - 省会 - 武汉市"); //设置字体(default,default-bold,monospace,serif,sans-serif)
ss1.setSpan(new TypefaceSpan("monospace"), 0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置字体大小(绝对值,单位:像素)
//第二个参数 如果为true,表示前面的字体大小单位为dip,否则为像素
ss1.setSpan(new AbsoluteSizeSpan(60), 0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.rgb(255,0,0)),1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.rgb(255,165,0)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.rgb(255,255,0)), 6, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.rgb(0,255,0)), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.rgb(0,127,255)), 11, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.rgb(0,0,255)), 12, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.rgb(139,0,255)), 13,
ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder1.append(ss1);
SpannableStringBuilder builder2 = new SpannableStringBuilder();
SpannableString ss2 = new SpannableString("浙江省 - 省会 - 杭州市"); //设置字体(default,default-bold,monospace,serif,sans-serif)
ss2.setSpan(new TypefaceSpan("monospace"), 0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss2.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss2.setSpan(new ForegroundColorSpan(Color.rgb(255,0,0)),1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss2.setSpan(new ForegroundColorSpan(Color.rgb(255,165,0)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss2.setSpan(new ForegroundColorSpan(Color.rgb(255,255,0)), 6, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss2.setSpan(new ForegroundColorSpan(Color.rgb(0,255,0)), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss2.setSpan(new ForegroundColorSpan(Color.rgb(0,127,255)), 11, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss2.setSpan(new ForegroundColorSpan(Color.rgb(0,0,255)), 12, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss2.setSpan(new ForegroundColorSpan(Color.rgb(139,0,255)), 13,
ss2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder2.append(ss2);
SpannableStringBuilder builder3 = new SpannableStringBuilder();
SpannableString ss3 = new SpannableString("江苏省 - 省会 - 南京市"); //设置字体(default,default-bold,monospace,serif,sans-serif)
ss3.setSpan(new TypefaceSpan("monospace"), 0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss3.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss3.setSpan(new ForegroundColorSpan(Color.rgb(255,0,0)),1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss3.setSpan(new ForegroundColorSpan(Color.rgb(255,165,0)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss3.setSpan(new ForegroundColorSpan(Color.rgb(255,255,0)), 6, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss3.setSpan(new ForegroundColorSpan(Color.rgb(0,255,0)), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss3.setSpan(new ForegroundColorSpan(Color.rgb(0,127,255)), 11, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss3.setSpan(new ForegroundColorSpan(Color.rgb(0,0,255)), 12, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss3.setSpan(new ForegroundColorSpan(Color.rgb(139,0,255)), 13,
ss3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder3.append(ss3);
SpannableStringBuilder builder4 = new SpannableStringBuilder();
SpannableString ss4 = new SpannableString("广东省 - 省会 - 广州市"); //设置字体(default,default-bold,monospace,serif,sans-serif)
ss4.setSpan(new TypefaceSpan("monospace"), 0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss4.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss4.setSpan(new ForegroundColorSpan(Color.rgb(255,0,0)),1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss4.setSpan(new ForegroundColorSpan(Color.rgb(255,165,0)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss4.setSpan(new ForegroundColorSpan(Color.rgb(255,255,0)), 6, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss4.setSpan(new ForegroundColorSpan(Color.rgb(0,255,0)), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss4.setSpan(new ForegroundColorSpan(Color.rgb(0,127,255)), 11, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss4.setSpan(new ForegroundColorSpan(Color.rgb(0,0,255)), 12, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss4.setSpan(new ForegroundColorSpan(Color.rgb(139,0,255)), 13,
ss4.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder4.append(ss4);
chooseTypeList.add(builder1);
chooseTypeList.add(builder2);
chooseTypeList.add(builder3);
chooseTypeList.add(builder4);
adapter = new ArrayAdapter<>( this, R.layout.select_dialog_singlechoice, R.id.text1, chooseTypeList);
} @Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setSingleChoiceItems(adapter, 0, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity3.this, items[which], Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
}
最终效果如下,是不是很酷炫?
原文链接:http://www.apkbus.com/blog-944603-77032.html
打开App,阅读手记