Android Studio平台中如何获得利用GridLayout布局的按钮Button的坐标啊?求大神赐教!!

发布网友

我来回答

1个回答

热心网友

参考下:http://tech.techweb.com.cn/thread-537085-1-1.html

代码布局应该差不多吧:
package com.example.dong;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.storage.OnObbStateChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;

public class N extends Activity{
public int images[] = new int[]{R.drawable.jingse,R.drawable.jingse1,
R.drawable.jingse2, R.drawable.jingse3, R.drawable.jingse4,
R.drawable.jingse5, R.drawable.jingse6, R.drawable.jingse7};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

LayoutParams param = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
ImageView image1 = new ImageView(this);
for(int i=0; i<images.length; i++){
image1.setBackgroundResource(images);
}
//Animation animation = AnimationUtils.loadAnimation(this, R.anim.trans);、、R.anim.trans为加载的动画布局文件
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(2000);
animation.setFillAfter(true);
animation.setRepeatCount(-1);
//其中,参数repeatCount为重复执行的次数。如果设置为n,则动画将执行n+1次。
// animation.initialize(2, 2, 30, 30);
image1.startAnimation(animation);
animation.startNow(); //启动动画(可省略)
// animation.cancel();//取消动画
layout.addView(image1, param);

ImageView image2 = new ImageView(this);
image2.setImageDrawable(this.getResources().getDrawable(R.drawable.jingse5));
layout.addView(image2);

ImageView image3 = new ImageView(this);
image3.setImageDrawable(this.getResources().getDrawable(R.drawable.jingse6));
// LayoutParams para;
// para = image.getLayoutParams();
//
// para.height = 300;
// para.width = 300;
// imageView.setLayoutParams(para)
LayoutParams p = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
p.width = 100;
p.height = 100;
image3.setLayoutParams(p);
// LinearLayout.LayoutParams pa = (LayoutParams) image3.getLayoutParams();
// pa.height = 300;
// pa.width = 300;
// image3.setLayoutParams(pa);
layout.addView(image3);

LinearLayout layout2 = new LinearLayout(this);
Button btn = new Button(this);
layout2.addView(btn, param);
layout.addView(layout2);
// layout.setBackgroundColor(Color.parseColor("#ffffff"));
LayoutParams params = new LayoutParams(
ScrollView.LayoutParams.FILL_PARENT,
ScrollView.LayoutParams.FILL_PARENT
);
ScrollView scro = new ScrollView(this);
scro.addView(layout, param);
//scro.setBackgroundColor(Color.parseColor("#F5F5DC"));
//scro.setBackgroundDrawable(getResources().getDrawable(R.drawable.jingse));
setContentView(scro);
}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com