我开始一个测试项目只是为了解决这个问题。没有对main.xml的更改。我想创建一个小部件大小的ImageView(80x100),其中包含从TextView转换的位图。是的,这听起来非常迂回,但这只是为了测试;最后,我希望ImageView有一个背景图像和多个TextViews。我不确定我做错了什么,但没有任何东西被推到屏幕上。
声明TextView / ImageView并在构造函数中传递“this”是一个问题吗?这是我的layoutParams的问题吗?这是代码:
package com.doaf.testproject;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TestProject extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100);
tv.setLayoutParams(layoutParams);
tv.setText("testing 1 2 3");
tv.setTextColor(0xFFFFFF);
tv.setBackgroundColor(0x555555);
Bitmap testB;
testB = loadBitmapFromView(tv);
ImageView iv = new ImageView(this);
iv.setLayoutParams(layoutParams);
iv.setBackgroundColor(0x555555);
iv.setImageBitmap(testB);
setContentView(iv);
}
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, 80, 100);
v.draw(c);
return b;
}
}
感谢您提供的任何帮助。我对Android比较陌生,而且很容易失去这个。
答案 0 :(得分:7)
我相信它会占用整个屏幕,因为你没有像Linear Layout这样的容器,它包含一个带有布局约束的ImageView,因此ImageView会扩展以填充可用的屏幕。试试这个:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100);
tv.setLayoutParams(layoutParams);
tv.setText("testing 1 2 3");
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(Color.TRANSPARENT);
Bitmap testB;
testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.layout(0, 0, 80, 100);
tv.draw(c);
ImageView iv = (ImageView)findViewById(R.id.menuIcon);
iv.setLayoutParams(layoutParams);
iv.setBackgroundColor(Color.GRAY);
iv.setImageBitmap(testB);
iv.setMaxHeight(80);
iv.setMaxWidth(80);
}
在你的main.xml文件中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="@+id/menuIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我不确定你想要达到什么目标,但我确信有更有效的方法来接近它。
答案 1 :(得分:1)
快速修改代码,试一试,看看它是否是你想要的结果:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100);
tv.setLayoutParams(layoutParams);
tv.setText("testing 1 2 3");
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(Color.TRANSPARENT);
Bitmap testB;
testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.layout(0, 0, 80, 100);
tv.draw(c);
ImageView iv = new ImageView(this);
iv.setLayoutParams(layoutParams);
iv.setBackgroundColor(Color.GRAY);
iv.setImageBitmap(testB);
setContentView(iv);
}
答案 2 :(得分:1)
在绘制之前,需要测量TextView(调用小节())并布局(调用布局())。你两个都没做。