当前位置: 编程技术>移动开发
本页文章导读:
▪上边图片和下面文字布局(命令) 上面图片和下面文字布局(命令)
LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams param = new LinearLayout.Layo.........
▪ 解决ActivityGroup的sub Activity中使用spinner出现的WindowManager$BadTokenException有关问题 解决ActivityGroup的sub Activity中使用spinner出现的WindowManager$BadTokenException问题
在ActivityGroup的sub activity中直接使用Spinner会遇到下面的异常信息: ERROR/AndroidRuntime(964): FATAL EXCEPTION: main ERROR/AndroidR.........
▪ 制造TextView的倒影 制作TextView的倒影
package com.javaeye.graphics;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;.........
[1]上边图片和下面文字布局(命令)
来源: 互联网 发布时间: 2014-02-18
上面图片和下面文字布局(命令)
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable .icon);
layout.addView(image, param);
TextView text;
text = new TextView(this);
text.setText("textview");
layout.addView(text, param);
setContentView(layout);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable .icon);
layout.addView(image, param);
TextView text;
text = new TextView(this);
text.setText("textview");
layout.addView(text, param);
setContentView(layout);
[2] 解决ActivityGroup的sub Activity中使用spinner出现的WindowManager$BadTokenException有关问题
来源: 互联网 发布时间: 2014-02-18
解决ActivityGroup的sub Activity中使用spinner出现的WindowManager$BadTokenException问题
在ActivityGroup的sub activity中直接使用Spinner会遇到下面的异常信息:
ERROR/AndroidRuntime(964): FATAL EXCEPTION: main
ERROR/AndroidRuntime(964):android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44f02d20 is not valid; is your activity running?
ERROR/AndroidRuntime(964): at android.view.ViewRoot.setView(ViewRoot.java:505)
ERROR/AndroidRuntime(964): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
.......
程序背景:使用TabHost,需要在子界面中添加Spinner按钮。
问题:Spinner按钮不能点击,点击出现unable to add window...is your activity running?的错误。
探索:
由于点击Spinner之后,会弹出一个dropdown item的选项卡,我认为unable to add window应该是这个选项卡没有可以添加上去的activity。从错误的最后一句也能看出程序认为它需要的那个activity并没有在运行。那Spinner究竟需要哪个activity?哪个activity是当前在运行的?
思前想后,没有想明白。在网上找了些资料,有一个博客上是这么说 AlertDialog.Builder(xxx.this) => AlertDialog.Builder(this.getParent()) 。我按照这个来试了一下,不加this.getParent()会出现上面的错误,加了就没问题。由此可见当前activity应该是tabhost,而不是每个tab对应的activity。
于是把代码改成下面这样子,注意红色部分:
View views = LayoutInflater.from(getParent()).inflate(R.layout.shiji_orderplace_view_0, null);
Spinner simple_input_5 = (Spinner) views.findViewById(R.id.simple_input_5);
String[] items= {"非连续", "连续"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item ,items);
adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
simple_input_5.setAdapter(adapter);
在ActivityGroup的sub activity中直接使用Spinner会遇到下面的异常信息:
ERROR/AndroidRuntime(964): FATAL EXCEPTION: main
ERROR/AndroidRuntime(964):android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44f02d20 is not valid; is your activity running?
ERROR/AndroidRuntime(964): at android.view.ViewRoot.setView(ViewRoot.java:505)
ERROR/AndroidRuntime(964): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
.......
程序背景:使用TabHost,需要在子界面中添加Spinner按钮。
问题:Spinner按钮不能点击,点击出现unable to add window...is your activity running?的错误。
探索:
由于点击Spinner之后,会弹出一个dropdown item的选项卡,我认为unable to add window应该是这个选项卡没有可以添加上去的activity。从错误的最后一句也能看出程序认为它需要的那个activity并没有在运行。那Spinner究竟需要哪个activity?哪个activity是当前在运行的?
思前想后,没有想明白。在网上找了些资料,有一个博客上是这么说 AlertDialog.Builder(xxx.this) => AlertDialog.Builder(this.getParent()) 。我按照这个来试了一下,不加this.getParent()会出现上面的错误,加了就没问题。由此可见当前activity应该是tabhost,而不是每个tab对应的activity。
于是把代码改成下面这样子,注意红色部分:
View views = LayoutInflater.from(getParent()).inflate(R.layout.shiji_orderplace_view_0, null);
Spinner simple_input_5 = (Spinner) views.findViewById(R.id.simple_input_5);
String[] items= {"非连续", "连续"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item ,items);
adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
simple_input_5.setAdapter(adapter);
[3] 制造TextView的倒影
来源: 互联网 发布时间: 2014-02-18
制作TextView的倒影
package com.javaeye.graphics; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuffXfermode; import android.graphics.Bitmap.Config; import android.graphics.PorterDuff.Mode; import android.graphics.Shader.TileMode; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class BitmapReflect extends Activity { private static final String TAG = "***BitmapFactoryOptionsTest***"; LinearLayout ly; TextView name; TextView phone; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); name = new TextView(this); phone = new TextView(this); name.setText("bruce zhang"); phone.setText("13739188962"); ly = new LinearLayout(this); ly.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); ly.setLayoutParams(llp); ly.addView(name); ly.addView(phone); Button button = new Button(this); button.setText("OK"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int width = phone.getWidth(); int height = name.getHeight() + phone.getHeight(); Log.e(TAG, "width: " + width + " height: " + height); Bitmap.Config bc = Bitmap.Config.ARGB_8888; Bitmap bitmap = Bitmap.createBitmap(width, height, bc); Canvas canvas = new Canvas(bitmap); ly.draw(canvas); Bitmap bitmap2 = createReflectedImage(bitmap); ImageView image = new ImageView(BitmapReflect.this); image.setBackgroundDrawable(new BitmapDrawable(bitmap2)); ly.addView(image); ly.postInvalidate(); } }); ly.addView(button); setContentView(ly); } public static Bitmap createReflectedImage(Bitmap originalImage) { final int reflectionGap = 4; //倒影和原图片间的距离 int width = originalImage.getWidth(); int height = originalImage.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); //倒影部分 Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); //要返回的倒影图片 Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); //画原来的图片 canvas.drawBitmap(originalImage, 0, 0, null); Paint defaultPaint = new Paint(); //倒影和原图片间的距离 canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); //画倒影部分 canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.MIRROR); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; } }
最新技术文章: