关于AlertDialog的自定义样式

仿造天天动听来编写自己的播放器的,当然功能远没有它的完善。最近想实现天天动听上的一个功能:在歌曲列表中的item添加一个按钮,点击此按钮弹出一个AlertDialog,该AlertDialog中有一个TextView,下面是一个ListView。Textview用来显示该item所对应的歌曲的名称,ListView则是一些选项(歌曲信息,移除,设为铃声什么的):

关于AlertDialog的自定义样式 - peculiar - art of devil
 
       好吧,刚刚用hierarchyviewer看了以下天天动听的布局,发现是个PopupWindow。

       我用的是自定义的一个AlertDialog。但是从昨天开始直到现在,我才获得了自己想要的效果。
       关于如何将ListView绑定到AlertDialog上,可以用以下几种方法实现:
       一、 AlertDialog. Builder.setItems( CharSequence[] items,  OnClickListener listener)。items就是你想要添加到AlertDialog的一个list,listener是为list设置的监听器,你只需在里面添加自己想要的动作即可。该方法比较简单。
       二、 AlertDialog.Builder.setAdapter ( ListAdapter adapter,  DialogInterface.OnClickListenerlistener)。这里你需要先定义一个ListAdapter,Adapter可以说是将数据绑定到UI的桥梁,功能很强大,listener与第一种方法里的一样。而且对于我这种菜鸟来说掌握它的用法与比较难。
       关于如何自定义AlertDialog样式:
       一、 AlertDialog. Builder.setView( View view)。这个view是事先从定义好的XML文件里获取的,关于如何获取可以用下面代码实现:
 
 

LayoutInflater inflater = LayoutInflater.from(Context context); View view = inflater.inflate(R.layout.alertdialog, null);//这里的R.layout.alertdialog即为你自定义的布局文件

       二、view. Window .setContentView( View  view)。主要代码如下:
 
  

AlertDialog mAlertDialog = builder.create(); mAlertDialog.show(); mAlertDialog.getWindow().setContentView(view);

      关于两者的区别,大家可以看这个链接: http://www.x2x1.com/show/6040883.aspx
      大致就是 setView()只会覆盖 AlertDialog的Title与 Button之间的那部分 setContentView()则会覆盖全部。但是否真的是这样呢?还有待验证。
      还有一点要注意的是: setContentView()必须放在 show()的后面,不然会报错。如果你要在代码里修改AlertDialog的大小,可以用以下代码实现:
 
  

mAlertDialog.getWindow().setLayout(150, 320);

      但是这段代码同样需要放在 show()的后面,不然你的改动会没有效果。

      如果可以成功地完成上述步骤,你差不多就可以自定义一个自己想要的AlertDialog了。但遗憾的是,我始终不知道怎么自定义AlertDialog的Title。最后我在 http://blog.csdn.net/chuekup/article/details/8018513找到了一些思路:
      1. 先自定义一个AlertDialog布局alertdialog.xml,包括两部分:一个textView 用来显示Title;一个ListView显示相关的选项:
 
  

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/alertdialog" android:layout_width="match_parent" android:layout_height="40dp" android:orientation="vertical" > <TextView android:id="@+id/titleView" android:layout_width="match_parent" android:layout_height="40dp" android:background="#006600" android:gravity="center" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusable="true" android:selectAllOnFocus="true" /> <ListView android:id="@id/android:list" android:layout_width="match_parent"

android:layout_height="wrap_content" android:background="#666633" android:gravity="center" /> </LinearLayout>

      2.定义一个item.xml,这是ListView中每一项的布局:
 
  

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text2" android:layout_width="150dip" android:layout_height="40dip" android:layout_gravity="right" android:textSize="18dip" android:textColor="#000000" android:background="#b0556B2F" android:gravity="center_vertical" />

      3.在java代码中:
 
  

private void showAlertDialog() { String[] itemList = {"设为铃声", "移除", "歌曲信息", "添加到..."}; ListAdapter mAdapter = new ArrayAdapter(context, R.layout.item, itemList); LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.alertdialog, null); TextView titleView = (TextView)view.findViewById(R.id.titleView); String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); titleView.setText(title); ListView listview = (ListView)view.findViewById(android.R.id.list); listview.setAdapter(mAdapter); listview.setOnItemClickListener(listener); AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog mAlertDialog = builder.create(); mAlertDialog.show(); mAlertDialog.getWindow().setContentView(view);

mAlertDialog.getWindow().setLayout(150, 320); }

        最后效果:
关于AlertDialog的自定义样式 - peculiar - art of devil
        
        当然还有很多地方有待优化,比如说,控制AlertDialog显示的位。
        不得不说这UI做的还是蛮丑的,自己审美观问题吧。但至少不是系统自带的样式了。
发布了18 篇原创文章 · 获赞 15 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/Alex_wsc/article/details/49472557
今日推荐