你知道http://code.google.com/p/mobile-anarchy-widgets/wiki/JoystickView的这个JoystickView吗? 好吧,我试图实现它。没有问题。我无法改变它的大小,因为我以编程方式添加了它。我以某种方式找到了如何改变大小,但现在它被卡在左上角,我发现的三个小时的一切都让我在LayoutParams param上出现了NullPointerException我已经创建或被拒绝因为它不是& #cast; t castable或者开头的东西。
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);
}
}
现在问我的问题:你能以某种方式以编程方式设置此视图的重力吗?
答案 0 :(得分:2)
你可以试试这个
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(500,500);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
Joystick.setLayoutParams(params);
答案 1 :(得分:0)
尝试将重心设置为FrameLayout并使用ViewGroup.LayoutParams设置操纵杆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));
答案 2 :(得分:0)
试试这个。
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) Joystick.getLayoutParams();
params.gravity = Gravity.TOP;// desired gravity
Joystick.setLayoutParams(params);
不要更改你的代码,只需在setContentView(gameScreen);
希望它有效之后添加它
答案 3 :(得分:0)
嗯,我找到了答案,但这是一种完全不同的方法,因为上述内容或我在别处找到的都不适合我。
我在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);
现在我在XML布局上获得了操纵杆,但它实际上是有效的(至少在我没有其他工作的情况下),并且在布局等方面进行更改非常容易。