在android中加载图像和文本

贾廷
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jatin.foreignlanguagefinal.French.FrenchFacts">

<ImageView
    android:id="@+id/imageFact"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:src="@drawable/booksf" />

<TextView
    android:id="@+id/textFact"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:layout_below="@+id/imageFact"
    android:gravity="center"
    android:maxLines="200"
    android:scrollbars="vertical"
    android:text="France is the world's most popular tourist destination – some 83.7 million visitors arrived in France, according to the World Tourism Organization report published in 2014, making it the world's most-visited country."
    android:textColor="@color/colorPrimary"
    android:textSize="20dp" />

<Button
    android:id="@+id/nextFact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textFact"
    android:layout_centerInParent="true"
    android:layout_margin="3dp"
    android:text="Next" />
</RelativeLayout>

public class FrenchFacts extends AppCompatActivity {

ArrayList<String> facts;
ImageView imageFacts;
TextView textFacts;
Button nextFact;
Map<String,Integer> mapFacts;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_french_facts);

    imageFacts = (ImageView) findViewById(R.id.imageFact);
    textFacts = (TextView) findViewById(R.id.textFact);
    nextFact = (Button) findViewById(R.id.nextFact)

    mapFacts = new HashMap<>();
    mapFacts.put("France is the world's most popular tourist destination – some 83.7 million visitors arrived in France, according to the World Tourism Organization report published in 2014, making it the world's most-visited country.",R.drawable.frances1);
    mapFacts.put("France is the largest country in the EU, and known as 'the hexagon' – with an area of 551,000 sq km it's almost a fifth of the EU’s total area, and due to its six-sided shape France is sometimes referred to as l’hexagone. About a quarter is covered by forest; only Sweden and Finland have more.",R.drawable.frances2 );
    mapFacts.put("Louis XIX was the king of France for just 20 minutes, the shortest ever reign – he ascended to the French throne in July 1830 after his father Charles X abdicated, and abdicated himself 20 minutes later in favour of his nephew, the Duke of Bordeaux.",R.drawable.frances3);
    mapFacts.put("Liberté, égalitié, fraternité meaning ‘liberty, equality and fraternity’ (or brotherhood) is the national motto of France – it first appeared around the time of the Revolution (1789–1799), and was written into the constitutions of 1946 and 1958. Today you’ll see it on coins, postage stamps and government logos often alongside ‘Marianne’ who symbolises the ‘triumph of the Republic’.",R.drawable.frances4);
    mapFacts.put("The French Army was the first to use camouflage in 1915 (World War I) – the word camouflage came from the French verb ‘to make up for the stage’. Guns and vehicles were painted by artists called camofleurs.",R.drawable.frances5);
    mapFacts.put("In France you can marry a dead person – under French law, in exceptional cases you can marry posthumously, as long as you can also prove that the deceased had the intention of marrying while alive and you receive permission from the French president.",R.drawable.frances6);
    mapFacts.put("France was the first country in the world to ban supermarkets from throwing away or destroying unsold food – since February 2016, shops must donate wastage to food banks or charities.",R.drawable.frances7);
    mapFacts.put("The Académie Française has aimed to preserve the French language since 1634 – by attempting to ban, somewhat unsuccessfully, foreign words such as blog, hashtag, parking, email and weekend. It was started by a small group of French intellects and officially recognised by Louis XIII in 1635.",R.drawable.frances8);
    mapFacts.put("At least 40 percent of the music on private radio stations must be of French origin – since 1996, the country’s top media regulator the Conseil Supérieur de L’Audiovisuel (CSA) has been charged with enforcing this French law. The CSA also requires half of the French music quota to be less than six months old.",R.drawable.frances9);
    mapFacts.put("France legalised same-sex marriage in 2013 – when President Françoise Holland signed the bill into law on 18 May 2013, France became the ninth country in Europe and 14th in the world to legalise same-sex marriage.",R.drawable.frances10);

nextFact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Random random = new Random();

            int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
            //String holder = facts.get(random.nextInt(facts.size()));
            textFacts.setMovementMethod(new ScrollingMovementMethod());





            for (Map.Entry<String,Integer> entry : mapFacts.entrySet()) {
                String text = entry.getKey();
                textFacts.setText(text);


                textFacts.setTextColor(color);

                int image = entry.getValue();

                Glide.with(getApplicationContext())
                        .load(image)
                            .into(imageFacts);


            }

        }


    });

我有一个图像视图、文本视图和按钮。单击按钮时,应显示不同的图像和文本。我曾尝试使用 hashmap,但在迭代时,按钮单击只能工作一次。之后无论什么图像和文字都不会改变。所以,请告诉我还有什么其他方式可以显示文本和图像。

PS:我在网上搜索了很多,都没有找到答案。请详细评论解决方案和代码。任何帮助表示赞赏。

苏菲汗

已编辑

 nextFact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Random random = new Random();
            int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));

        textFacts.setMovementMethod(new ScrollingMovementMethod());

        int i = random.nextInt(mapFacts.size());
        Object[] keys = map.keySet().toArray();
        String text = (String) keys[i];
        int id = mapFacts.get(keys[i]);
        textFacts.setText(text);
        textFacts.setTextColor(color);
        Glide.with(getApplicationContext()).load(id).into(imageFacts);
    }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Swift 在 webview 中加载 html 文件,更改图像大小、字体和文本大小

来自分类Dev

在ListView Android中加载图像

来自分类Dev

在android imageview中加载图像

来自分类Dev

图像和文本分别在listview中显示-Android使用图像加载器

来自分类Dev

在加载aspx页面时显示加载图像和文本

来自分类Dev

如何使用angularjs在图像旁边加载图像和文本

来自分类Dev

在Android中共享图像和文本

来自分类Dev

在Android中加载非常大的图像

来自分类Dev

图像未在Flutter Android中加载

来自分类Dev

以编程方式在Android中加载图像

来自分类Dev

[React Native]图像无法在Android中加载

来自分类Dev

如何在Android中设置图像的背景和文本/图像

来自分类Dev

Android自定义文本视图以保存图像和文本

来自分类Dev

Android Async在Listview中加载图像-图像闪烁

来自分类Dev

分层图像和文本

来自分类Dev

在RecycleView中加载图像

来自分类Dev

在bPopup中加载图像

来自分类Dev

在JAR中加载图像

来自分类Dev

在WatchKit中加载图像

来自分类Dev

在Android上将图像和文本共享到Facebook

来自分类Dev

Android-具有图像和文本的RelativeLayout,设计问题

来自分类Dev

Android-RecyclerView从JSON设置图像和文本

来自分类Dev

Android-RecyclerView从JSON设置图像和文本

来自分类Dev

想要在android中并排显示图像和文本

来自分类Dev

带有图像和文本的 ScrollView 在 android 中不滚动

来自分类Dev

如何知道在Android中加载大位图图像的正确尺寸?

来自分类Dev

使用Universal Image Loader Android在ListView中加载错误的图像?

来自分类Dev

无法使用Picasso库在android中加载图像

来自分类Dev

Android:使用Picasso延迟在horizontalscrollview中加载图像

Related 相关文章

热门标签

归档