背景色の設定

広告

背景色を設定する方法を確認します。「FrameLayout」クラスの親クラスである「View」クラスで用意されている「setBackgroundColor」メソッドを使います。

Sets the background color for this view.

Parameters:
  color  the color of the background

1番目の引数に背景色を指定します。色の指定には「android.graphics.Color」クラスを使います。詳しくは「Colorクラス」を参照して下さい。

具体的には次のように記述します。

private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 

@Override public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    FrameLayout frameLayout = new FrameLayout(this);
    frameLayout.setBackgroundColor(Color.GRAY);
    setContentView(frameLayout);

    Button button = new Button(this);
    button.setText("Button");
    frameLayout.addView(button, new ViewGroup.LayoutParams(WC, WC));

    TextView text = new TextView(this);
    text.setText("TextView");
    text.setTextColor(Color.RED);
    frameLayout.addView(text, new ViewGroup.LayoutParams(WC, WC));
}

サンプルプログラム

それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。

Test07_01.java

package jp.javadrive.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.graphics.Color;
import android.view.View.OnClickListener;

public class Test07_01 extends Activity implements OnClickListener
{
    private final int FP = ViewGroup.LayoutParams.FILL_PARENT; 
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 

    private Button buttonText;
    private Button buttonButton;
    private FrameLayout frameLayout;

    @Override public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);


        LinearLayout btnLinearLayout = new LinearLayout(this);
        btnLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

        buttonText = new Button(this);
        buttonText.setText("Text");
        buttonText.setOnClickListener(this);

        buttonButton = new Button(this);
        buttonButton.setText("Button");
        buttonButton.setOnClickListener(this);

        btnLinearLayout.addView(buttonText, createParam(WC, WC));
        btnLinearLayout.addView(buttonButton, createParam(WC, WC));

        frameLayout = new FrameLayout(this);
        frameLayout.setBackgroundColor(Color.GRAY);

        linearLayout.addView(btnLinearLayout, createParam(FP, WC));
        linearLayout.addView(frameLayout, createParam(200, 100));
    }

    private LinearLayout.LayoutParams createParam(int w, int h){
        return new LinearLayout.LayoutParams(w, h);
    }

    private ViewGroup.LayoutParams createViewGroupParam(int w, int h){
        return new ViewGroup.LayoutParams(w, h);
    }

    public void onClick(View v) {
        if (v == buttonText){
            TextView text = new TextView(this);
            text.setText("TextView");
            text.setTextColor(Color.RED);
            frameLayout.addView(text, createViewGroupParam(WC, WC));
        }else if (v == buttonButton){
            Button button = new Button(this);
            button.setText("Button");
            frameLayout.addView(button, createViewGroupParam(WC, WC));
        }
    }
}

ビルド後にエミュレーター上で実行します。

背景色の設定

今回はFrameLayoutのサイズを固定で指定したうえで背景色を設定しています。ボタンをクリックすると子ビューを追加することができます。

背景色の設定

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。