添加网站链接到ImageView?

时间:2014-01-02 23:22:43

标签: android url imageview

是否可以(以及如何)将ImageView链接到网页,以便当用户点击图片时,它会将他们带到网页?我已经建立了这个结构,我需要每个ImageView的Web链接:

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

</ScrollView>

3 个答案:

答案 0 :(得分:7)

您只需要一个onClick个活动来处理所有ImageView次点击。

使用android:tag属性指定要打开的网址 使用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 :(得分:0)

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

请参阅此链接:How can ImageView link to web page?

答案 2 :(得分:0)

基本上添加一个点击监听器,当按下图像时,转到具有意图的网页。

--- Cristian的代码(How can ImageView link to web page?

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

});