简体   繁体   中英

How can I set the LayoutParams of an View Programmatically?

You know this JoystickView from http://code.google.com/p/mobile-anarchy-widgets/wiki/JoystickView ? Well, I tried to implement it. No Problems with it. I couldn't change It's size because I Added It programmatically. I somehow found out how to change the size, but now It's stuck in the upper left corner and everything I found for three hours got me a NullPointerException on the LayoutParams param I've created or was rejected because It wasn't castable or something to begin with.

public class GameActivity extends Activity implements JoystickMovedListener{

private GraphicsHolder mGraphicsHolder;
private String TAG = "GameActivity";

/*
 * creates a new GraphicsHolder and sets it its ContentView
 * the GraphicsThread is being included in the GraphicsHolder
 */
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Log.d(TAG, "created");

    FrameLayout gameScreen = new FrameLayout(this);

    mGraphicsHolder = new GraphicsHolder(this);

    JoystickView Joystick = new JoystickView(this);


    // I need to set the Gravity HERE


    Joystick.setLayoutParams(new RelativeLayout.MarginLayoutParams(500,500));

    gameScreen.addView(mGraphicsHolder);
    gameScreen.addView(Joystick);

    setContentView(gameScreen);
}
}

Now Here's my Question: can you somehow set the Gravity of this View programmatically?

You can try this

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(500,500);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

Joystick.setLayoutParams(params);

Try to set gravity center to FrameLayout and use ViewGroup.LayoutParams set Joystick LayoutParams :

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
gameScreen.setLayoutParams(params);

Joystick.setLayoutParams(new ViewGroup.LayoutParams(500,500));

try this.

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) Joystick.getLayoutParams();
params.gravity = Gravity.TOP;// desired gravity

Joystick.setLayoutParams(params);

dont change ur code just add this after setContentView(gameScreen); hope it works

Well, I found the answer, but it's a completely different approach, since none of the above or those I found elsewhere worked for me.

I did the following changes in the onCreate method:

    mGraphicsHolder = new GraphicsHolder(this);

    LayoutInflater inflate = (LayoutInflater)
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    setContentView(mGraphicsHolder);

    getWindow().addContentView(inflate.inflate(
        R.layout.controllayout, null), new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT, 
        ViewGroup.LayoutParams.FILL_PARENT));

    Joystick = (JoystickView) getWindow().findViewById(R.id.joystickView1);
    Joystick.setOnJostickMovedListener(this);

Now I got my Joystick on an XML Layout, but It actually works (at least in my case where nothing else worked), and it's quite easy to make changes in things of Layout etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM