自定义Android吐丝

      本人还是学习Android方面的新手,每学习一个东西,把学习过程记录下来方便以后查阅,还是挺好的。好了废话不多说.....

        看到一个带图片的Toast,的Demo觉得比较新颖,虽然很少看到有人用到项目里。但还是先研究一下:

        看了一下,主要是重新定义了一个需要的布局,然后转换成View,在设置到Toast里即可,跟我们自定义对话框一样一样的:

 

       View view = getLayoutInflater().inflate(R.layout.toast, null);
       TextView textview = (TextView) view.findViewById(R.id.textview);
       textview.setText("这里是Toast显示的\n提示信息......");
       Toast toast = new Toast(this);
       toast.setDuration(Toast.LENGTH_LONG);
       toast.setView(view);
       toast.show();


        在下面是布局这个Toast布局文件代码:

 

   

     

<?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:layout_margin="10dp"
    android:background="#555"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />

</LinearLayout>

 

 

 

下面是效果:

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义 Android DialogPreference,您可以按照以下步骤进行操作: 1. 创建一个新的类,扩展 DialogPreference 类,并实现构造函数和 onDialogClosed() 方法。 ```java public class CustomDialogPreference extends DialogPreference { public CustomDialogPreference(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (positiveResult) { // 在此处保存用户的选择 } } } ``` 2. 在 res/values/ 文件夹下创建一个名为 attrs.xml 的文件,在其中定义自定义属性。 ```xml <resources> <declare-styleable name="CustomDialogPreference"> <attr name="custom_attr" format="string" /> </declare-styleable> </resources> ``` 3. 在 res/layout/ 文件夹下创建一个 XML 布局文件,定义您的自定义对话框布局。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 在此处添加您的布局元素 --> </LinearLayout> ``` 4. 在 CustomDialogPreference 类中添加 showDialog() 方法,并在其中加载您的自定义对话框布局。 ```java public class CustomDialogPreference extends DialogPreference { public CustomDialogPreference(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (positiveResult) { // 在此处保存用户的选择 } } @Override protected void showDialog(Bundle state) { super.showDialog(state); Dialog dialog = getDialog(); dialog.setContentView(R.layout.custom_dialog_layout); // 在此处添加逻辑来初始化对话框元素 } } ``` 5. 在您的首选项 XML 文件中使用 CustomDialogPreference。 ```xml <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <com.example.CustomDialogPreference android:key="custom_dialog_preference" android:title="Custom Dialog Preference" custom_attr="some value" /> </PreferenceScreen> ``` 注意:在步骤 5 中,您需要将 com.example 替换为您的应用程序的包名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值