【问题标题】:Android user interface with bitmaps problem带有位图问题的 Android 用户界面
【发布时间】:2011-02-17 20:01:07
【问题描述】:

我有一个扩展 View 的 Ball 类。在里面我给出了一些特性并实现了 onTouchEvent(),所以我可以处理运动。我还使用 onDraw,所以我可以绘制球的位图。在我的活动类中,我创建了一个新布局,并将视图添加到其中以便显示。一切正常,除非我尝试在我的布局中添加更多球,但它们没有出现!总是显示第一个添加到布局中的球! 这是活动类的 onCreate 代码:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
    int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;

    Point point1 = new Point();
    point1.x = 50;
    point1.y = 20;
    Point point2 = new Point();
    point2.x = 100;
    point2.y = 20;
    Point point3 = new Point();
    point3.x = 150;
    point3.y = 20;

    ColorBall ball1 = new ColorBall(this,R.drawable.bol_groen, point1);
    ll.addView(ball1, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);

    ColorBall ball2 = new ColorBall(this,R.drawable.bol_rood, point2);
    ll.addView(ball2, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);

    ColorBall ball3 = new ColorBall(this,R.drawable.bol_blauw, point3);

    ll.addView(ball3, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);        

}

可能是什么问题?我也试过了,最后只有一个 setContentView()。我想我不能使用布局,所以我可以绘制自定义视图中的位图!我是对? 我是否应该更改我的代码并创建一个视图,并在其中创建一个包含我想要显示的所有球的数组,然后将此视图设置为从我的主要活动类中显示?(就像这个 setContentView(customview))。

【问题讨论】:

    标签: android user-interface layout custom-view


    【解决方案1】:

    您多次调用setContentView,而预计每次活动初始化只调用一次。

    更新:

    您可以使用这种布局 xml 代替您使用的编程方式吗?这只是为了 100% 确定要添加 ColorBalls 的容器没有问题。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:id="@+id/container"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" />
    

    以防万一,这里是将其包含在活动中的代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.your_name_of_layout);
    
        LinearLayout container = (LinearLayout) findViewById(R.id.container);
        ..
        container.addView(ball1);
        container.addView(ball2);
        ..
    }
    

    【讨论】:

    • 是的,你是对的,但这不是问题,我也尝试删除 setContentViews 并只保留最后一个,但没有任何改变。我仍然无法画出所有的球!
    • @stelios:另一个想法 - 尝试将 ll.setOrientation(LinearLayout.HORIZONTAL); 更改为 ll.setOrientation(LinearLayout.VERTICAL);
    • 我也尝试过这个...我在想我不能在布局中添加多个自定义视图...我知道这听起来不对但是在哪里问题?!!!!
    • @stelios:您是否尝试仅添加 point3 - 它是否可见,是否在预期坐标处?
    • 是的,我也试过这个,它工作正常。如果我只添加一个自定义视图并不重要,但只有一个没关系....如果你想要我可以添加和剩下的代码!
    猜你喜欢
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2015-03-24
    • 2012-07-06
    • 2021-01-03
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多