Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android以编程方式在TextView中添加按钮_Android - Fatal编程技术网

Android以编程方式在TextView中添加按钮

Android以编程方式在TextView中添加按钮,android,Android,我想在文本视图中的文本中间添加按钮,比如当我在文本中看到电话号码时,用文本视图中的按钮替换它,并将按钮的背景设置为联系人图片,但我如何才能将按钮添加到文本视图?可能吗 <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/b

我想在文本视图中的文本中间添加按钮,比如当我在文本中看到电话号码时,用文本视图中的按钮替换它,并将按钮的背景设置为联系人图片,但我如何才能将按钮添加到文本视图?可能吗

 <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

您所说的是在小部件中放置widgetor视图。这是不可能的。您只能在布局中添加小部件。但是,如果要使用相对布局,可以将一个小部件放置在另一个小部件的顶部


参考:developer.android.com/guide/topics/ui/declaration layout.html

我认为您不需要文本视图中的按钮。它是视图的组合,意味着视图组就足够了 你会做这项工作的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="normaltext" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/contactpicture"
    android:text="phonenumber"
    android:visibility="invisible" />
根据文本更改文本视图和按钮的可见性。
希望它能帮助您。谢谢。基本上,您不能在文本视图中放置任何按钮。解决这类问题的一种方法是以编程方式将文本划分为不同的部分,即在遇到数字时在文本视图之间添加一个按钮。假设你有一个文本:号码123456是我的电话号码,你需要分成三部分:号码123456是我的电话号码,并相应地创建小部件,即

文本视图是文本,而不是按钮最好隐藏或显示视图。假设,如果任何情况下您不需要该按钮,请按程序设置VisibilityView.gone,并添加文本视图,将可见性隐藏到gone,并在需要时显示所需的视图谢谢@HarshParikh,请参阅vishal Answer中的我的评论更好地发挥可见性,在相对布局中为联系人图片拍摄imageview或imagebutton。谢谢@vishal,但我不知道它可能位于何处,是否可以捕捉文本中电话号码的位置,并将其设置为另一布局的边距?可能位于TextView右侧或其他位置,并使用imagebutton。每当TextView找到电话号码时,ImageButton的图像就会被替换。简单,如果其他人会这样做,我希望。我没有得到这一部分。有可能捕捉到文本中电话号码的位置,并将其设置为另一个布局的边距吗?好的,我接受你的答案,因为有可能在文本视图中放置一个小部件