0

我必须调整其中一个视图的大小或两者兼而有之......似乎一旦setContentView被称为新的setCotentView就不能被调用。我试图通过再次创建整个布局来调整内容的大小。

请告诉我该怎么做...

这是我到目前为止的代码:

package v.w;

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


public class VariableWidthActivity extends Activity {
    /** Called when the activity is first created. */
    LinearLayout RLGreen;
    LinearLayout RLChange;
    Button btnClick;
    LinearLayout RLYellow ;
    Button  btClick;

        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RLChange = new LinearLayout(this);        
        RLChange.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                                 LayoutParams.FILL_PARENT)); 
        RLChange.setOrientation(LinearLayout.VERTICAL);

        RLGreen = new LinearLayout(this);        
        RLGreen.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                                 LayoutParams.FILL_PARENT));
        RLGreen.setBackgroundColor(Color.GREEN);
        RLGreen.setOrientation(LinearLayout.HORIZONTAL);
        btnClick = new Button(this);        
        btnClick.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT) );
        //btnClick.setBackgroundColor(Color.RED);
        btnClick.setText("BUTTON");


        RLYellow = new LinearLayout(this);        
        RLYellow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                                 200));
        RLYellow.setBackgroundColor(Color.YELLOW);
        RLChange.addView(RLGreen);
        RLGreen.addView(RLYellow);

        RLGreen.addView(btnClick);


    }
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();

            setContentView(RLChange);
            btClick=new Button(this);
            btClick.setText("BUTTON");
            btClick.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT) );
            //btnClick.setBackgroundColor(Color.RED);
            RLYellow.addView(btClick);
            btClick.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) { 
                    //RLChange.forceLayout();
                    RLChange.removeAllViews();

                    RLChange.addView(RLYellow);
                    RLChange.addView(RLGreen);
                    RLGreen.addView(btnClick);
                    setContentView(RLChange);
                }
             });  

        }

}
4

0 回答 0