最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用CardView實現(xiàn)圓角對話框

 更新時間:2018年11月28日 17:26:38   作者:ruancw  
這篇文章主要為大家詳細介紹了Android使用CardView實現(xiàn)圓角對話框,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言:隨著用戶體驗的不斷的加深,良好的UI視覺效果也必不可少,以前方方正正的對話框樣式在APP已不復存在,取而代之的是帶有圓角效果的Dialog,之前設(shè)置對畫框的圓角效果都是通過drawable/shape屬性來完成,隨著Google API的不斷更新,API 21(Android 5.0)添加了新的控件CardView,這使得圓角的實現(xiàn)更加方便快捷。

效果圖:

導入CardView依賴(API 21新控件)

implementation 'com.android.support:cardview-v7:26.1.0'

1.布局引用

<android.support.v7.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:cardCornerRadius="@dimen/dp_10">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
      android:id="@+id/tv_title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/colorTabClick"
      android:gravity="center"
      android:padding="@dimen/dp_10"
      android:text="溫馨提示:確定修改維護詳情信息?"
      android:textColor="@color/bg_mainWhite"
      android:textSize="@dimen/dp_16" />

    <View
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="@color/bg_line" />

    <TextView
      android:id="@+id/tv_des"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="@dimen/dp_10"
      android:gravity="top"
      />

    <View
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="@color/bg_line" />

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:orientation="horizontal">

      <TextView
        android:id="@+id/tv_cancel"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="取消"
        android:textSize="@dimen/dp_16" />

      <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@color/bg_line" />

      <TextView
        android:id="@+id/tv_confirm"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="確定"
        android:textSize="@dimen/dp_16" />
    </LinearLayout>
  </LinearLayout>

</android.support.v7.widget.CardView>

1.cardCornerRadius屬性:設(shè)置圓角的弧度大小,這里設(shè)置的為10dp

2.CardView還有padding、cardUseCompatPadding(內(nèi)邊距)、background等屬性

3.CardView繼承自FrameLayout,使用時可以重新嵌套布局

2.代碼實現(xiàn)

/**
 * 展示對話框
 */
private void showDialog(String title) {
  //初始化布局文件
  View dialogView = View.inflate(mContext, R.layout.dialog_layout_test, null);
  //標題
  TextView tvTitle = (TextView) dialogView.findViewById(R.id.tv_title);
  //確定按鈕
  TextView tvConfirm = (TextView) dialogView.findViewById(R.id.tv_confirm);
  //取消按鈕
  TextView tvCancel = (TextView) dialogView.findViewById(R.id.tv_cancel);
  //描述信息
  TextView tvDes= (TextView) dialogView.findViewById(R.id.tv_des);
  //設(shè)置標題及描述信息
  tvTitle.setText(title);
  tvDes.setText("退出當前登錄后將要重新登錄!");
  //確定和取消按鈕監(jiān)聽事件
  tvConfirm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      Intent intent = new Intent(mContext,LoginActivity.class);
      startActivity(intent);
      UIUtil.toast("退出成功,請重新登錄");
      getActivity().finish();
      mDialog.dismiss();
    }
  });
  tvCancel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      mDialog.dismiss();
    }
  });
  mMessageBuilder = new AlertDialog.Builder(mContext);
  mDialog = mMessageBuilder.create();
  //設(shè)置背景色為透明,解決設(shè)置圓角后有白色直角的問題
  Window window=mDialog.getWindow();
  window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  mDialog.setView(dialogView);
  mDialog.setCanceledOnTouchOutside(false);//點擊屏幕不消失
  mDialog.show();
  //設(shè)置參數(shù)必須在show之后,不然沒有效果
  WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
  mDialog.getWindow().setAttributes(params);
}

使用的是V7包的AlertDialog實現(xiàn)的,當然也可以使用Dialog實現(xiàn)。

總結(jié):CardView實現(xiàn)對話框的圓角效果更加的方便,不用編寫shape屬性,當標題欄需要背景色時,也無需考慮設(shè)置標題欄的shape(不使用CardView時,如果不使用shape設(shè)置背景色,會導致左上和右上不會變成圓角)。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android LineChart繪制多條曲線的方法

    Android LineChart繪制多條曲線的方法

    這篇文章主要為大家詳細介紹了Android LineChart繪制多條曲線的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 創(chuàng)建Android庫的方法及Android .aar文件用法小結(jié)

    創(chuàng)建Android庫的方法及Android .aar文件用法小結(jié)

    本文給大家介紹了創(chuàng)建Android庫的方法及Android中 .aar文件生成方法與用法詳解,涉及到創(chuàng)建庫模塊操作步驟及開發(fā)注意事項,需要的朋友參考下吧
    2017-12-12
  • Android實現(xiàn)記住賬號密碼功能

    Android實現(xiàn)記住賬號密碼功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)記住賬號密碼功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android亮屏和熄屏控制實例詳解

    Android亮屏和熄屏控制實例詳解

    這篇文章主要介紹了Android亮屏和熄屏控制的方法,結(jié)合實例形式較為詳細的分析了Android亮屏與息屏的原理,實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下
    2016-02-02
  • Android 使用PDF.js瀏覽pdf的方法示例

    Android 使用PDF.js瀏覽pdf的方法示例

    這篇文章主要介紹了Android 使用PDF.js瀏覽pdf的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • 詳解android 用webview加載網(wǎng)頁(https和http)

    詳解android 用webview加載網(wǎng)頁(https和http)

    這篇文章主要介紹了詳解android 用webview加載網(wǎng)頁(https和http),詳細的介紹了兩個錯誤的解決方法,有興趣的可以了解一下
    2017-11-11
  • Android 編程下的計時器代碼

    Android 編程下的計時器代碼

    在安卓 APP 的手機號注冊邏輯中,經(jīng)常會有將激活碼發(fā)送到手機的環(huán)節(jié),這個環(huán)節(jié)中絕大多數(shù)的應(yīng)用考慮到網(wǎng)絡(luò)延遲或服務(wù)器壓力以及短信服務(wù)商的延遲等原因,會給用戶提供一個重新獲取激活碼的按鈕
    2013-08-08
  • android中Intent傳值與Bundle傳值的區(qū)別詳解

    android中Intent傳值與Bundle傳值的區(qū)別詳解

    本篇文章是對android中Intent傳值與Bundle傳值的區(qū)別進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • android layout XML解析錯誤的解決方法

    android layout XML解析錯誤的解決方法

    從別的地方復制過來XML時,layout預(yù)覽時提示解析錯誤。
    2013-04-04
  • Android二維碼創(chuàng)建實例

    Android二維碼創(chuàng)建實例

    這篇文章主要介紹了Android二維碼創(chuàng)建實例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論

香港| 乌鲁木齐市| 惠水县| 宁安市| 永德县| 科技| 远安县| 怀化市| 古交市| 大足县| 新建县| 乌拉特后旗| 手游| 东安县| 烟台市| 凭祥市| 玉田县| 五莲县| 两当县| 石台县| 渭源县| 通道| 澎湖县| 丰城市| 淮北市| 樟树市| 吉木乃县| 文安县| 武山县| 西乡县| 肇庆市| 灌云县| 乳源| 衡东县| 古田县| 佛学| 宜川县| 林州市| 桦甸市| 易门县| 铁岭市|