android - 将网页链接添加到 ImageView?

标签 android url imageview

是否有可能(以及如何)将 ImageView 链接到网页,以便当用户单击图像时将他们带到网页?我已经制作了这个结构,我需要每个 ImageView 的网络链接:

<ScrollView
 <LinearLayout     
  <ImageView1
  <ImageView2
  <ImageView3
    .  
    . 
    .
 </LinearLayout>

</ScrollView>

最佳答案

您只需要一个onClick 事件即可处理所有ImageView 点击。

使用android:tag属性来指定你想要打开的url。
使用 android:onClick 属性指定处理点击事件的方法。

在 xml 中:

<ImageView
    android:id="@+id/image1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/yourimage1" 
    android:tag="http://site_1.com"
    android:onClick="openBrowser"/>  

<ImageView
    android:id="@+id/image2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/yourimage2" 
    android:tag="http://site_2.com"
    android:onClick="openBrowser"/>  

Activity 中:

public void openBrowser(View view){

    //Get url from tag
    String url = (String)view.getTag();

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);

    //pass the url to intent data
    intent.setData(Uri.parse(url));

    startActivity(intent);
}

关于android - 将网页链接添加到 ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20893803/

相关文章:

java - 在 Android 问题中将 Base64 字符串转换为位图

android - ImageView背景

android - 远程内容不刷新

android - 在 DatePickerDialog Android 中禁用今天日期之前的日期?

mysql - 将mysql数据库表中的目录路径替换为url路径

c# - URL 的正则表达式,包括查询字符串

java - 如何在 IE 选项卡而不是新窗口中打开 URL - Java

android - 为什么AndroidStudio3.0启用的高级分析要求API级别低于26

android如何在canvas半透明上绘制位图

android - 如何使用不同的 LayoutParams