单击按钮时,我想知道如何在布局中放置一些图像

时间:2015-09-30 06:43:48

标签: java android android-layout

我想放置图片。 单击图像时,将在frameLayout中绘制。

如果按下猫图像,就会画出猫。

这是我的代码,图像放在一个可绘制的文件夹中。

MainActivity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ImageButton mStamp, mFrame, mConfirmBtn, mCat, mMush;

    HorizontalScrollView horizontalScrollView;
    FrameLayout frameLayout;

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

        mStamp = (ImageButton)findViewById(R.id.stamp);
        mFrame = (ImageButton)findViewById(R.id.frame);
        mConfirmBtn = (ImageButton)findViewById(R.id.confirm);
        mCat = (ImageButton)findViewById(R.id.cat);
        mMush = (ImageButton)findViewById(R.id.mush);

        mStamp.setOnClickListener(this);
        mFrame.setOnClickListener(this);
        mConfirmBtn.setOnClickListener(this);
        mCat.setOnClickListener(this);
        mMush.setOnClickListener(this);

        horizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);
        frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.stamp :
                horizontalScrollView.setVisibility(View.VISIBLE);
                break;

            case R.id.frame :
                horizontalScrollView.setVisibility(View.GONE);
                break;

            case R.id.confirm :
                // Intent intent = new Intent(MainActivity.this, ConfirmActivity.class);
                // startActivity(intent);
                break;

            case R.id.cat :
                horizontalScrollView.setVisibility(View.GONE);
                break;

            case R.id.mush :
                horizontalScrollView.setVisibility(View.GONE);
                break;
        }
    }
}

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="@android:color/black">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_above="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageButton
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/cat"
                android:id="@+id/cat"
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <ImageButton
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/mush"
                android:id="@+id/mush"
                android:layout_width="40dp"
                android:layout_height="40dp" />
        </LinearLayout>
    </HorizontalScrollView>


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:background="@android:color/transparent"
            android:scaleType="fitCenter"
            android:src="@drawable/stamp"
            android:id="@+id/stamp"
            android:layout_width="60dp"
            android:layout_height="60dp" />

        <ImageButton
            android:background="@android:color/transparent"
            android:scaleType="fitCenter"
            android:src="@drawable/frame"
            android:id="@+id/frame"
            android:layout_width="60dp"
            android:layout_height="60dp" />

        <ImageButton
            android:background="@android:color/transparent"
            android:scaleType="fitCenter"
            android:src="@drawable/confirm"
            android:id="@+id/confirm"
            android:layout_width="60dp"
            android:layout_height="60dp" />
    </LinearLayout>


</RelativeLayout>

在res文件夹中绘制

cat.png
confirm.png
frame.png
mush.png
stamp.png
请给我一些提示。

2 个答案:

答案 0 :(得分:0)

这里有什么问题? - 老板7分钟前

在FrameLayout中使用'ImageView'并在按钮点击上设置其来源 - Shahzeb 6分钟前

伙计们。非常好的提示。

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ImageButton mStamp, mFrame, mConfirmBtn, mCat, mMush;

    HorizontalScrollView horizontalScrollView;
    FrameLayout frameLayout;

    ImageView imageView1, imageView2;

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

        mStamp = (ImageButton)findViewById(R.id.stamp);
        mFrame = (ImageButton)findViewById(R.id.frame);
        mConfirmBtn = (ImageButton)findViewById(R.id.confirm);
        mCat = (ImageButton)findViewById(R.id.cat);
        mMush = (ImageButton)findViewById(R.id.mush);

        mStamp.setOnClickListener(this);
        mFrame.setOnClickListener(this);
        mConfirmBtn.setOnClickListener(this);
        mCat.setOnClickListener(this);
        mMush.setOnClickListener(this);

        imageView1 = new ImageView(this);
        imageView2 = new ImageView(this);

        horizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);
        frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.stamp :
                horizontalScrollView.setVisibility(View.VISIBLE);
                break;

            case R.id.frame :
                horizontalScrollView.setVisibility(View.GONE);
                break;

            case R.id.confirm :
                // Intent intent = new Intent(MainActivity.this, ConfirmActivity.class);
                // startActivity(intent);
                break;

            case R.id.cat :
                horizontalScrollView.setVisibility(View.GONE);
                imageView1.setImageResource(R.drawable.cat);
                frameLayout.addView(imageView1);

                frameLayout.removeView(imageView2);
                break;

            case R.id.mush :
                horizontalScrollView.setVisibility(View.GONE);
                imageView2.setImageResource(R.drawable.mush);
                frameLayout.addView(imageView2);

                frameLayout.removeView(imageView1);
                break;
        }
    }
}

现在......我想移动这些图片..

答案 1 :(得分:0)

如果要通过按钮单击在布局中添加图像,请在布局中设置图像视图。你没有完全描述你的意思,但我在这里有一些代码,它可能有所帮助。这是图像按钮

public void onClick(View view) {
    switch(view.getId()){

        case R.id.stamp :
            mStamp.setImageResource(R.drawable.yourimagename);
            horizontalScrollView.setVisibility(View.VISIBLE);
            break;

    }
}