【问题标题】:Nothing Showing When Dynamically Creating a Relative Layout动态创建相对布局时没有显示
【发布时间】:2012-04-17 01:36:22
【问题描述】:

您好,我的代码一直存在空指针异常问题(请参阅我的其他问题),因此我已经着手动态创建视图。

这是我的代码:

public class HarvestLifeActivity extends Activity implements OnTouchListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //add touch listeners for buttons : assign each a name and link it.
    View MoveUp = findViewById(R.id.up);
    MoveUp.setOnTouchListener(this); // use (this) to link to the current onTouchListener references in class declaration above.
    View MoveDown = findViewById(R.id.down);
    MoveDown.setOnTouchListener(this);
    View MoveLeft = findViewById(R.id.left);
    MoveLeft.setOnTouchListener(this);
    View MoveRight = findViewById(R.id.right);
    MoveRight.setOnTouchListener(this);

}

@Override
protected void onStart() {
    super.onStart();

    Drawable standing = getResources().getDrawable(R.drawable.test_circle);
    //code to convert character size from pixel to dp
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float logicalDensity = metrics.density;

    int pxToConv = 50;
    int convToDp = (int) (pxToConv / logicalDensity + 0.5);

    RelativeLayout characterContainer = new RelativeLayout(this);
    RelativeLayout.LayoutParams characParams = new RelativeLayout.LayoutParams(convToDp, convToDp);
    characParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    characterContainer.setBackgroundDrawable(standing);
    characterContainer.setId(101);




}
// Process button clicks

public boolean onTouch(View v, MotionEvent event) {
    switch(v.getId()){
        case R.id.up:
            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                //as long as up button is pressed it will keep moving character up
               while (event.getAction() == MotionEvent.ACTION_DOWN)
               {    
                    RelativeLayout characterContainer = (RelativeLayout) findViewById(101);
                    Drawable walking = getResources().getDrawable(R.drawable.test_circle_red);
                    int startX = characterContainer.getLeft();
                    int startY = characterContainer.getTop();
                    int defaultWidth = characterContainer.getWidth();
                    int defaultHeight = characterContainer.getHeight();

                        //create new position for character 1 pixel closer to the top of screen
                        int newX = startX - 1;
                        int newY = startY;

                        //remove character
                        RelativeLayout view = (RelativeLayout) characterContainer;
                        ViewGroup owner = (ViewGroup) view.getParent();
                        owner.removeView(view);

                        //re make character in new position created above and assign background as walking forwards animation
                        RelativeLayout.LayoutParams characParams = new RelativeLayout.LayoutParams(defaultWidth,defaultHeight);
                        characParams.leftMargin = newY;
                        characParams.topMargin = newX;
                        characterContainer.setLayoutParams(characParams);           
                        characterContainer.setBackgroundDrawable(walking); 
                        }

                    break;

                    // when button is let go of

                case MotionEvent.ACTION_UP:

                    RelativeLayout characterContainer = (RelativeLayout) findViewById(101);
                    Drawable standing = getResources().getDrawable(R.drawable.test_circle);
                    characterContainer.setBackgroundDrawable(standing);

                    default:
                    break;
                    }
                    return true;

           case R.id.down:
               .. ..
            }
    return true;
    }
}

当我运行它时,创建的布局不存在。

有什么我错过的吗?

【问题讨论】:

    标签: java android android-layout android-widget


    【解决方案1】:

    您正在创建一个 RelativeLayout 对象,但从未将其设置为窗口的内容...

    让你的 onCreate 看起来像这样:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Cut these lines out of onStart();
        RelativeLayout characterContainer = new RelativeLayout(this);
        RelativeLayout.LayoutParams characParams = new RelativeLayout.LayoutParams(convToDp, convToDp);
        characParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        characterContainer.setBackgroundDrawable(standing);
        characterContainer.setId(101);
    
        setContentView(characterContainer);
    
        //add touch listeners for buttons : assign each a name and link it.
        /*View MoveUp = findViewById(R.id.up);
        MoveUp.setOnTouchListener(this); // use (this) to link to the current onTouchListener references in class declaration above.
        View MoveDown = findViewById(R.id.down);
        MoveDown.setOnTouchListener(this);
        View MoveLeft = findViewById(R.id.left);
        MoveLeft.setOnTouchListener(this);
        View MoveRight = findViewById(R.id.right);
        MoveRight.setOnTouchListener(this);*/
    
    }
    

    【讨论】:

      【解决方案2】:

      您没有将创建的布局添加到现有视图中。

      那是你的问题。

      addview(your_layout);
      

      【讨论】:

        猜你喜欢
        • 2019-01-30
        • 1970-01-01
        • 2021-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多