如何在每个 imagview 下放置文本

发布于 2024-12-07 06:18:57 字数 6394 浏览 3 评论 0原文

在我的项目中,我使用图像作为水平滚动视图中的按钮。当我单击图像时,它会显示水平滚动视图上方的图像。我想要的是当我单击图像时在图像下方显示描述文本。有人可以帮忙吗?

这是我的 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="match_parent" android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical" android:background="@color/white">

<ImageView android:layout_gravity="center" android:src="@drawable/a1"
    android:layout_height="200dp" android:id="@+id/IVdisplay"
    android:layout_width="200dp"></ImageView>

<TextView android:layout_width="wrap_content"
    android:id="@+id/Aciklama"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:background="#AA000000" android:textColor="#ffffffff"
    android:text="fghhhhhhhhhhhhhhtyuyjhjkghklşklşkl" 
     />
<HorizontalScrollView android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_gravity="bottom">
    <LinearLayout android:layout_gravity="bottom"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <ImageView android:src="@drawable/a1"
            android:layout_height="125dp" android:id="@+id/IVimage1"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a2"
            android:layout_height="125dp" android:id="@+id/IVimage2"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a3"
            android:layout_height="125dp" android:id="@+id/IVimage3"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a4"
            android:layout_height="125dp" android:id="@+id/IVimage4"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a5"
            android:layout_height="125dp" android:id="@+id/IVimage5"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a6"
            android:layout_height="125dp" android:id="@+id/IVimage6"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a7"
            android:layout_height="125dp" android:id="@+id/IVimage7"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a8"
            android:layout_height="125dp" android:id="@+id/IVimage8"
            android:layout_width="125dp" android:padding="15dp">
</ImageView>
</LinearLayout></HorizontalScrollView></LinearLayout>

这是我的java文件。

    import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;

import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;


public class varningsmarken extends Activity implements View, OnClickListener {

    ImageView display;
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.varningsmarken);
AdView ad = (AdView) findViewById(R.id.adView2);
ad.loadAd(new AdRequest());

    display = (ImageView) findViewById (R.id.IVdisplay);
    ImageView image1 = (ImageView) findViewById (R.id.IVimage1);
    ImageView image2 = (ImageView) findViewById (R.id.IVimage2);
    ImageView image3 = (ImageView) findViewById (R.id.IVimage3);
    ImageView image4 = (ImageView) findViewById (R.id.IVimage4);
    ImageView image5 = (ImageView) findViewById (R.id.IVimage5);
    ImageView image6 = (ImageView) findViewById (R.id.IVimage6);
    ImageView image7 = (ImageView) findViewById (R.id.IVimage7);
    ImageView image8 = (ImageView) findViewById (R.id.IVimage8);
    ImageView image9 = (ImageView) findViewById (R.id.IVimage9);
    ImageView image10 = (ImageView) findViewById (R.id.IVimage10);
    ImageView image11 = (ImageView) findViewById (R.id.IVimage11);
    ImageView image12 = (ImageView) findViewById (R.id.IVimage12);
    ImageView image13 = (ImageView) findViewById (R.id.IVimage13);
    ImageView image14 = (ImageView) findViewById (R.id.IVimage14);
    ImageView image15 = (ImageView) findViewById (R.id.IVimage15);
    ImageView image16 = (ImageView) findViewById (R.id.IVimage16);
    ImageView image17 = (ImageView) findViewById (R.id.IVimage17);
    ImageView image18 = (ImageView) findViewById (R.id.IVimage18);
    ImageView image19 = (ImageView) findViewById (R.id.IVimage19);
    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    image6.setOnClickListener(this);
    image7.setOnClickListener(this);
    image8.setOnClickListener(this);
    image9.setOnClickListener(this);
    image10.setOnClickListener(this);
    image11.setOnClickListener(this);
    image12.setOnClickListener(this);
    image13.setOnClickListener(this);
    image14.setOnClickListener(this);
    image15.setOnClickListener(this);
    image16.setOnClickListener(this);
    image17.setOnClickListener(this);
    image18.setOnClickListener(this);
    image19.setOnClickListener(this);
public void onClick(android.view.View v) {


switch (v.getId()){

case R.id.IVimage1:
    display.setImageResource(R.drawable.a1);
break;

case R.id.IVimage2:
    display.setImageResource(R.drawable.a2);
    break;
case R.id.IVimage3:
    display.setImageResource(R.drawable.a3);
    break;
case R.id.IVimage4:
    display.setImageResource(R.drawable.a4);
    break;
case R.id.IVimage5:
    display.setImageResource(R.drawable.a5);
break;
case R.id.IVimage6:
    display.setImageResource(R.drawable.a6);
    break;
case R.id.IVimage7:
    display.setImageResource(R.drawable.a7);
    break;
case R.id.IVimage8:
    display.setImageResource(R.drawable.a8);
    break;

    }
}
}

In my project i am using images as button in horizontalscrollview. When i click an image it shows the image above the horizantalscrollview. What i want is to show a description text under the image when i click it. Can anyhone help?

Here is my xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="match_parent" android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical" android:background="@color/white">

<ImageView android:layout_gravity="center" android:src="@drawable/a1"
    android:layout_height="200dp" android:id="@+id/IVdisplay"
    android:layout_width="200dp"></ImageView>

<TextView android:layout_width="wrap_content"
    android:id="@+id/Aciklama"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:background="#AA000000" android:textColor="#ffffffff"
    android:text="fghhhhhhhhhhhhhhtyuyjhjkghklşklşkl" 
     />
<HorizontalScrollView android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_gravity="bottom">
    <LinearLayout android:layout_gravity="bottom"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <ImageView android:src="@drawable/a1"
            android:layout_height="125dp" android:id="@+id/IVimage1"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a2"
            android:layout_height="125dp" android:id="@+id/IVimage2"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a3"
            android:layout_height="125dp" android:id="@+id/IVimage3"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a4"
            android:layout_height="125dp" android:id="@+id/IVimage4"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a5"
            android:layout_height="125dp" android:id="@+id/IVimage5"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a6"
            android:layout_height="125dp" android:id="@+id/IVimage6"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a7"
            android:layout_height="125dp" android:id="@+id/IVimage7"
            android:layout_width="125dp" android:padding="15dp"></ImageView>
        <ImageView android:src="@drawable/a8"
            android:layout_height="125dp" android:id="@+id/IVimage8"
            android:layout_width="125dp" android:padding="15dp">
</ImageView>
</LinearLayout></HorizontalScrollView></LinearLayout>

Here is my java file.

    import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;

import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;


public class varningsmarken extends Activity implements View, OnClickListener {

    ImageView display;
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.varningsmarken);
AdView ad = (AdView) findViewById(R.id.adView2);
ad.loadAd(new AdRequest());

    display = (ImageView) findViewById (R.id.IVdisplay);
    ImageView image1 = (ImageView) findViewById (R.id.IVimage1);
    ImageView image2 = (ImageView) findViewById (R.id.IVimage2);
    ImageView image3 = (ImageView) findViewById (R.id.IVimage3);
    ImageView image4 = (ImageView) findViewById (R.id.IVimage4);
    ImageView image5 = (ImageView) findViewById (R.id.IVimage5);
    ImageView image6 = (ImageView) findViewById (R.id.IVimage6);
    ImageView image7 = (ImageView) findViewById (R.id.IVimage7);
    ImageView image8 = (ImageView) findViewById (R.id.IVimage8);
    ImageView image9 = (ImageView) findViewById (R.id.IVimage9);
    ImageView image10 = (ImageView) findViewById (R.id.IVimage10);
    ImageView image11 = (ImageView) findViewById (R.id.IVimage11);
    ImageView image12 = (ImageView) findViewById (R.id.IVimage12);
    ImageView image13 = (ImageView) findViewById (R.id.IVimage13);
    ImageView image14 = (ImageView) findViewById (R.id.IVimage14);
    ImageView image15 = (ImageView) findViewById (R.id.IVimage15);
    ImageView image16 = (ImageView) findViewById (R.id.IVimage16);
    ImageView image17 = (ImageView) findViewById (R.id.IVimage17);
    ImageView image18 = (ImageView) findViewById (R.id.IVimage18);
    ImageView image19 = (ImageView) findViewById (R.id.IVimage19);
    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    image6.setOnClickListener(this);
    image7.setOnClickListener(this);
    image8.setOnClickListener(this);
    image9.setOnClickListener(this);
    image10.setOnClickListener(this);
    image11.setOnClickListener(this);
    image12.setOnClickListener(this);
    image13.setOnClickListener(this);
    image14.setOnClickListener(this);
    image15.setOnClickListener(this);
    image16.setOnClickListener(this);
    image17.setOnClickListener(this);
    image18.setOnClickListener(this);
    image19.setOnClickListener(this);
public void onClick(android.view.View v) {


switch (v.getId()){

case R.id.IVimage1:
    display.setImageResource(R.drawable.a1);
break;

case R.id.IVimage2:
    display.setImageResource(R.drawable.a2);
    break;
case R.id.IVimage3:
    display.setImageResource(R.drawable.a3);
    break;
case R.id.IVimage4:
    display.setImageResource(R.drawable.a4);
    break;
case R.id.IVimage5:
    display.setImageResource(R.drawable.a5);
break;
case R.id.IVimage6:
    display.setImageResource(R.drawable.a6);
    break;
case R.id.IVimage7:
    display.setImageResource(R.drawable.a7);
    break;
case R.id.IVimage8:
    display.setImageResource(R.drawable.a8);
    break;

    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情栀口红 2024-12-14 06:18:57

您可以执行类似的操作,就像

TextView desc = (TextView)findViewById(R.id.Aciklama);
desc.setText(DESCRIPTION);

您对显示图像视图所做的那样。创建对文本视图的引用并在单击时对其设置文本。

You can do something like

TextView desc = (TextView)findViewById(R.id.Aciklama);
desc.setText(DESCRIPTION);

You do exactly what you did with the display imageview. Create a reference to the textview and setText to it on click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文