如何在行中多次显示相同的图像?

时间:2012-10-15 08:09:44

标签: android image android-layout

我正在为儿童开发一个应用程序。儿童学习基本数学的乐趣。

在这个应用程序中,我使用苹果图像在屏幕上显示。当它们给出第一个输入和第二个输入时,它应该显示第一个和第二个输入中指定的苹果图像。

是否有任何人知道这个或任何有源代码帮助我的解决方案

1 个答案:

答案 0 :(得分:2)

这是你可以做到的方式,虽然很简单:)

你的xml应该是这样的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/input1" />
 <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/input2" />
 <Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="Result"/>
<HorizontalScrollView 
android:id="@+id/scroll1" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<LinearLayout android:id="@+id/linear1"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView 
android:id="@+id/scroll2" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout 
    android:id="@+id/linear2"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView 
android:id="@+id/scroll3" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout 
    android:id="@+id/linear3"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

你的活动看起来像这样

package com.example.stackanswer;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class AppleActivity extends Activity {

private LinearLayout linear1;
private LinearLayout linear2;
private LinearLayout linear3;
private HorizontalScrollView scrollbar1;
private HorizontalScrollView scrollbar2;
private HorizontalScrollView scrollbar3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apple);
    final EditText input1 = (EditText)findViewById(R.id.input1);
    final EditText input2 = (EditText)findViewById(R.id.input2);
    scrollbar1 = (HorizontalScrollView)findViewById(R.id.scroll1);
    scrollbar2 = (HorizontalScrollView)findViewById(R.id.scroll2);
    scrollbar3 = (HorizontalScrollView)findViewById(R.id.scroll3);
      linear1 = (LinearLayout)findViewById(R.id.linear1);
      linear2 = (LinearLayout)findViewById(R.id.linear2);
      linear3 = (LinearLayout)findViewById(R.id.linear3);
    Button output = (Button)findViewById(R.id.button);
    output.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String input1A = input1.getText().toString();
            String input1B = input2.getText().toString();
            int value1 = Integer.parseInt(input1A);
            int value2 = Integer.parseInt(input1B);
            Log.i("1",""+value1);
            Log.i("2",""+value2);
            linear1.removeAllViews();
            linear2.removeAllViews();
            linear3.removeAllViews();
            linear1.setScrollContainer(true);
            ImageView image ;
            for(int i=0;i<value1;i++){

                image = new ImageView(getApplicationContext());
                image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) );

                image.setImageResource(R.drawable.ic_launcher);
                Log.i("i", i+"");
                linear1.addView(image);
                scrollbar1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            }
            for(int i=0;i<value2;i++){
                image = new ImageView(getApplicationContext());
                image.setImageResource(R.drawable.ic_launcher);
                Log.i("i", i+"");
                linear2.addView(image);
                scrollbar2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            }
            int sum = value1+value2;
            for(int i=0;i<sum;i++){
                image = new ImageView(getApplicationContext());
                image.setImageResource(R.drawable.ic_launcher);
                Log.i("i", i+"");
                linear3.addView(image);
                scrollbar3.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            }
        }
    });

}

}

完整的源代码here