简体   繁体   English

如何以编程方式设置视图的LayoutParams?

[英]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 ? 您从http://code.google.com/p/mobile-anarchy-widgets/wiki/JoystickView知道此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. 我以某种方式找到了更改大小的方法,但是现在它卡在了左上角,我发现三个小时的所有内容都使我在我创建或被拒绝的LayoutParams参数上出现了NullPointerException异常,因为它不可铸造或其他首先。

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并使用ViewGroup.LayoutParams设置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); 不要更改您的代码,只需在setContentView(gameScreen);之后添加此代码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: 我在onCreate方法中做了以下更改:

    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. 现在,我将操纵杆安装在XML布局上,但是它实际上可以工作(至少在我没有其他工作的情况下),并且可以很容易地对Layout等进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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