将Web链接添加到ImageView吗?

问题描述 投票:3回答:3

是否可以(以及如何)将ImageView链接到网页,以便用户单击图像时将其带到网页?我已经建立了这种结构,并且需要每个ImageView的Web链接:

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

</ScrollView>
android url imageview
3个回答
6
投票

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

使用android:tag属性来分配要打开的URL。使用android:onClick属性分配处理click事件的方法。

在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"/>  

活动中:

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);
}

1
投票

基本上添加点击侦听器,并在按下图像时有意地转到网页。

ImageView img = (ImageView)findViewById(R.id.foo_bar);
img.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://casidiablo.net"));
        startActivity(intent);
    }
});

原始来源: How can ImageView link to web page?


0
投票

是,最简单的方法是使用ImageButton。在ImageButton的onClick方法中,发送带有URL的Intent以打开浏览器或您自己的WebView。如果您不希望它看起来像一个按钮,只需在ImageView上设置onClickListener。

查看此链接:How can ImageView link to web page?

© www.soinside.com 2019 - 2024. All rights reserved.