设计TextView,使其看起来像一个AlertDialog。

huangapple go评论60阅读模式
英文:

Design TextView to look like a AlertDialog

问题

我正在尝试创建一个新的 TextView,它看起来像一个具有 AlertDialogTheme 样式的 AlertDialog,使用以下构造函数:

public TextView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

我已经尝试将 R.attr.alertDialogTheme 添加到参数 defStyleAttr,将 R.style.AlertDialogTheme 添加到参数 defStyleRes,但没有成功。我是否错误地使用了这些值?提前感谢!

英文:

I'm trying to create a new TextView that looks like a AlertDialog with a style of AlertDialogTheme using this constructor

public TextView (Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes)

I tried already adding R.attr.alertDialogTheme to the parameter defStyleAttr and R.style.AlertDialogTheme to parameter defStyleRes but it didn't work. Am I using the values wrong? Thanks in advance!

答案1

得分: 0

以下是翻译好的部分:

第一,你可以创建自己的TextView并使其中的一部分文本链接化:

val message = TextView(context)
// R.string.dialog_message => "Test this dialog following the link to google.com"
val spannable = SpannableString(R.string.dialog_message) 
Linkify.addLinks(spannable, Linkify.WEB_URLS)
message.text = spannable 
message.movementMethod = LinkMovementMethod.getInstance()

返回AlertDialog
.Builder(context)
.setView(message)
.create()

第二,你可以在你的AlertDialog消息中使用类似以下的HTML文本:

Html.fromHtml("<a href='https://play.google.com/store/apps/details?id=com.xxxxxxxxx'>Click Here</a>")

第三,你可以创建一个扩展Dialog类的DialogFragment,并填充你自己的视图,以下是如何做的参考链接:

如何在Android中使用DialogFragment

如果有帮助,请告诉我。

英文:

There are multiple things you could try out:

First you could create your own textview and make linkify the part of your textview

    val message = TextView(context)
    // R.string.dialog_message =&gt;
    // &quot;Test this dialog following the link to google.com&quot;
    val spannable = SpannableString(R.string.dialog_messag) 
    Linking.addLinks(spannable, 
    Linkify.WEB_URLS)
    message.text = spannable 
    message.movementMethod = 
    LinkMovementMethod.getInstance()

    Return Alertdialog
    .Builder(context)
    .setView(message)
    .create()

Secondly you could use in your alertDialogs message an html text like this:

Html.fromHtml(&quot;&lt;a href=&#39;https://play.google.com/store/apps/details?id=com.xxxxxxxxx&#39;&gt;Click Here&lt;/a&gt;&quot;)

Third you could create a dialogFragment which extends the Dialog class and inflate your own view, here is a reference on how to do that:

https://medium.com/@huseyinozkoc/how-to-use-dialogfragment-in-android-f8095dd0993f

Let me know if it helped

huangapple
  • 本文由 发表于 2023年1月9日 18:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定