読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - StackView

 

activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<StackView
android:id="@+id/stackView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></StackView>
</LinearLayout>
 
MainActivity.java
package com.example.liguofeng.stackview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.StackView;

public class MainActivity extends AppCompatActivity {
// Imageリソース
int[] images = {
R.drawable.baiyang, R.drawable.chunv, R.drawable.jinniu,
R.drawable.juxie, R.drawable.mojie, R.drawable.sheshou,
R.drawable.shizi, R.drawable.shuangyu, R.drawable.shuangzi,
R.drawable.shuiping, R.drawable.tiancheng, R.drawable.tianxie,
};

StackView stackView;

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

stackView = (StackView) findViewById(R.id.stackView);

BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return images.length;
}

@Override
public Object getItem(int position) {
return images[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// return ImageView
ImageView imageView = new ImageView(MainActivity.this);

imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);

imageView.setLayoutParams(new ViewGroup.LayoutParams(

RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
));

return imageView;
}
};
// adapter設定
stackView.setAdapter(adapter);
}
}