【问题标题】:main.xml hiding the view helpmain.xml 隐藏视图帮助
【发布时间】:2011-03-26 14:11:32
【问题描述】:

我无法同时显示 setContentView(R.layout.main) 和 View。我认为我没有正确理解这个概念。谁能解释我哪里出错了。谢谢你。 //请阅读代码中的注释

我正在尝试使用 BitmapFactory 在 main.xml 上显示图像。

   public class TryGraph extends Activity 
 {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//I think this is where I need your help
    setContentView(new myView(this));//I want this to be displayed in main.xml
}

private class myView extends View{

    public myView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sinewave);
        Bitmap resizedBitmap = Bitmap.createBitmap(myBitmap, 0, 0,
                300, 143);
        canvas.drawBitmap(resizedBitmap, 60, 50, null);
        Paint myPaint = new Paint();
        myPaint.setColor(Color.RED);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(5);
        canvas.drawRect(250,255,260,250, myPaint);

    }
}

}

XML 文件是

【问题讨论】:

    标签: android android-emulator android-ndk android-manifest


    【解决方案1】:

    当您调用setContentView 时,您是在告诉设备加载应该向用户显示的整个布局。在大多数情况下,此视图将占据整个屏幕。现在这个 Layout 文件被认为是根并且可能包含子视图,其中之一应该是您的 ImageView。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView   
        android:id="@+id/banner"
        android:text="hello world"
        >  
    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sampleimage"
        />
    <?LinearLayout>
    

    现在可以通过代码使用findViewById(R.id.myImageView) 访问此ImageView,然后您可以使用BitmapFactory 设置您的图像。如果不打算改变图片,可以在布局文件中设置android:src="@drawable/sampleimage"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多