私は、RelativeLayoutを作成し、そこにボタンを追加するいくつかのコードを以下に示します。それは、onCreate()から作成されたときにうまく描画され、赤い背景に青色のボタンを表示します。ボタンを追加するとRelativeLayoutの背景が黒くなるのはなぜですか?
しかし、新しいReltativeLayoutを最初にクリックすると、新しいボタンが黒い背景に表示されるので、RelativeLayoutは表示されません。
面白いのは、ボタンを追加する行をコメントアウトすると機能し、何とかボタンが相対レイアウトに影響を与えているということです。
ありがとうございました。
package com.android.mikeviewtester;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
public class ViewTesterActivity extends Activity {
void createNewView(int bgColor, int btnColor) {
// create a new relative layout
android.widget.RelativeLayout newView = new android.widget.RelativeLayout(this);
// create a button
Button btn = new Button(this);
// set the background color
btn.setBackgroundColor(btnColor);
// create a layoutParams struct for adding the button to the relative layout view
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(100, 100);
// add the button to the relative layout
newView.addView(btn, params);
// set the relative layout background color
newView.setBackgroundColor(bgColor);
// set the ontouch listener for the relativeLayout
newView.setOnTouchListener((android.view.View.OnTouchListener) mOnTouchListener);
// create the layout to fill the activity
RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
// set the relative layout as the view
setContentView(newView, viewParams);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create and set the initial view
createNewView(Color.RED, Color.BLUE);
}
// ios - (void)buttonWasPressed:(id)whichButton {
private android.view.View.OnTouchListener mOnTouchListener = new android.view.View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (v != null)
v.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP) {
// create and set a new view
createNewView(Color.GREEN, Color.MAGENTA);
}
return true;
}
};
}
を呼び出す前に
v.setVisibility(View.GONE);
を追加するには、RelativeLayoutにviewParamsを設定する代わりに、()に動作し – Cata