是否有可能(以及如何)将 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/