基本的にはcssとかで使うときと同じ感覚で使えます。
まずはweightを指定しないとどうなるのか
ソース
package com.jp.layout;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout oLayout = new LinearLayout(getApplicationContext());
oLayout.addView( make_Image(R.drawable.ic_launcher));
oLayout.addView( make_Image(R.drawable.ic_launcher));
oLayout.addView( make_Image(R.drawable.ic_launcher));
oLayout.setOrientation(LinearLayout.HORIZONTAL);
setContentView(oLayout);
}
private ImageView make_Image(int ResID){
ImageView img = new ImageView(getApplicationContext());
Bitmap bmp = BitmapFactory.decodeResource(getResources(), ResID);
img.setImageDrawable(getResources().getDrawable(ResID));
img.setLayoutParams(new LinearLayout.LayoutParams(WC,WC));
return img;
}
}
このソースを
package com.jp.layout;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout oLayout = new LinearLayout(getApplicationContext());
oLayout.addView( make_Image(R.drawable.ic_launcher, 1));
oLayout.addView( make_Image(R.drawable.ic_launcher, 1));
oLayout.addView( make_Image(R.drawable.ic_launcher, 1));
oLayout.setOrientation(LinearLayout.HORIZONTAL);
setContentView(oLayout);
}
private ImageView make_Image(int ResID, int weight){
ImageView img = new ImageView(getApplicationContext());
Bitmap bmp = BitmapFactory.decodeResource(getResources(), ResID);
img.setImageDrawable(getResources().getDrawable(ResID));
img.setLayoutParams(new LinearLayout.LayoutParams(WC, WC, weight));
return img;
}
private ImageView make_Image(int ResID){
ImageView img = new ImageView(getApplicationContext());
Bitmap bmp = BitmapFactory.decodeResource(getResources(), ResID);
img.setImageDrawable(getResources().getDrawable(ResID));
img.setLayoutParams(new LinearLayout.LayoutParams(WC, WC));
return img;
}
}
ちゃんと均等に画像が割り振られていますね。
oLayout.addView( make_Image(R.drawable.ic_launcher, 1));
oLayout.addView( make_Image(R.drawable.ic_launcher, 1));
oLayout.addView( make_Image(R.drawable.ic_launcher, 1));
この処理で、すべての画像を1:1:1で表示しています。
weightをいじって
oLayout.addView( make_Image(R.drawable.ic_launcher, 1));
oLayout.addView( make_Image(R.drawable.ic_launcher, 1));
oLayout.addView( make_Image(R.drawable.ic_launcher, 2));
こんな感じになります。
わかるかな??
ちょっと比率がかわります。
0 件のコメント:
コメントを投稿