Android随机图片视图链接到匹配按钮

时间:2017-09-15 18:47:51

标签: android android-layout random

同时随机选择imageview一个按钮有真正的其他三个是错误的。 imageView随机显示照片但不调整按钮。我该怎么办?我做imageView随机选择但不调整其他。 see this image

RES /布局/ activity_letter_test_first

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/activity_letter_test_first"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.nayemuzzaman.learning.LetterTestFirst">


    <TextView
        android:text="Try to find A "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/a"
        android:id="@+id/imgViewRandom"
        android:onClick="imageLetterRandom" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:text="Button"
            android:layout_width="182dp"
            android:layout_height="wrap_content"
            android:id="@+id/button" />

        <Button
            android:text="Button"
            android:layout_width="171dp"
            android:layout_height="wrap_content"
            android:id="@+id/button4" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="59dp">

        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button5"
            android:layout_weight="1.14" />

        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button6"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

的java / com.example.nayemuzzaman.learning / LetterTestFirst

package com.example.nayemuzzaman.learning;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.util.Random;

public class LetterTestFirst extends AppCompatActivity {

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

        final int[] phtLetter={R.drawable.a, R.drawable.b, R.drawable.c}; //total picture add

        final ImageView imgViewRnd=(ImageView) findViewById(R.id.imgViewRandom); //imageView address

        final Random ranLtr=new Random();

        int imgLetter=ranLtr.nextInt(phtLetter.length);
        imgViewRnd.setImageResource(phtLetter[imgLetter]);

        imgViewRnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int tlLetter = ranLtr.nextInt(phtLetter.length); //total length letter photos & random selected photos.
                imgViewRnd.setImageResource(phtLetter[tlLetter]);
            }
        });

    }

}

1 个答案:

答案 0 :(得分:0)

尝试给出平衡的权重,例如;

<LinearLayout 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:id="@+id/activity_letter_test_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nayemuzzaman.learning.LetterTestFirst">


<TextView
    android:text="Try to find A "
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
   />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imgViewRandom"
   />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/button" />

    <Button
        android:text="Button"
        android:layout_width="171dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/button4" />
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:layout_weight="1" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button6"
        android:layout_weight="1" />
</LinearLayout>

顺便说一下,我不推荐使用静态dp之类的; &#34; 170dp&#34;等。