我正在以编程方式在主要布局上生成不同的UI TextView,并将其放入不同的边距w.r.t.在实现和测试方面,生成的所有文本视图都是重叠的。您能否告诉我将以编程方式生成的文本视图放到不同位置的内容?
以下是我的方法:
setLabel(lblPairing , "PAIRING" , (int)(0.22*screenWidth), (int)(0.075*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 40.0f );
public void setLabel(AutoResizeTextView tv , String text , int xpos , int ypos, int color,
String fontPath , int screenWidth , Context ctx , float size){
Typeface myTypefaceX = Typeface.createFromAsset(ctx.getAssets(), fontPath );
tv.setTypeface(myTypefaceX);
tv.setTextColor(color);
tv.setTextSize(size*screenWidth / 1080);
tv.setText(text);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(xpos, ypos, 0, 0);
tv.setLayoutParams(lp);
main.addView(tv);
System.out.println("xpos " + xpos );
System.out.println("ypos " + ypos);
}
以下是我的layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image_areas"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/shaded_region"
android:visibility="invisible" />
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/cover_1_d" />
<com.adapter.AutoResizeTextView
android:id="@+id/lblPairing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="78dp"
android:layout_marginTop="46dp"
android:singleLine="true"
android:text="Pairing"
android:textColor="#fff" />
.....
</RelativeLayout>
整个活动代码
public class CoverActivity extends Activity
implements View.OnTouchListener
{
/**
* Create the view for the activity.
*
*/
private AutoResizeTextView lblPairing;
private AutoResizeTextView lblCalibrate;
private AutoResizeTextView lblFlight1;
private AutoResizeTextView lblFlight2;
private AutoResizeTextView lblBluetooth;
private AutoResizeTextView lblBLEStatus;
private AutoResizeTextView lblFirmW;
private AutoResizeTextView lblFirmVersion;
private AutoResizeTextView lblnameTitle;
private AutoResizeTextView lblnameCustom;
private String versionNum;
private String nameDrone;
private Display display ;
private Point size;
private int screenWidth;
private int screenHeight;
private RelativeLayout main;
private Context mainCt;
private ImageView iv;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
screenWidth= size.x;
screenHeight = size.y;
System.out.println("screenWidth " + screenWidth );
System.out.println("screenHeight " + screenHeight);
setContentView(R.layout.activity_cover);
main = (RelativeLayout) this.findViewById(R.id.my_frame);
System.out.println("main " + main);
mainCt = this.getApplicationContext();
lblPairing = new AutoResizeTextView(this);
//new (AutoResizeTextView)findViewById(R.id.lblPairing);
lblCalibrate= new AutoResizeTextView(this);
//(AutoResizeTextView)this.findViewById(R.id.lblCalibrate);
/* lblFlight1= (AutoResizeTextView)this.findViewById(R.id.lblFlight1);
lblFlight2= (AutoResizeTextView)this.findViewById(R.id.lblFlight2);
lblBluetooth= (AutoResizeTextView)this.findViewById(R.id.lblBluetooth);
lblBLEStatus= (AutoResizeTextView)this.findViewById(R.id.lblBLEStatus);
lblFirmW= (AutoResizeTextView)this.findViewById(R.id.lblFirmW);
lblFirmVersion= (AutoResizeTextView)this.findViewById(R.id.lblFirmVersion);
lblnameTitle= (AutoResizeTextView)this.findViewById(R.id.lblnameTitle);
lblnameCustom= (AutoResizeTextView)this.findViewById(R.id.lblnameCustom); */
Typeface myTypefaceX = Typeface.createFromAsset(mainCt.getAssets(), "fonts/TitilliumText22L003.otf" ); //"fonts/TitilliumText22L003.otf");
lblPairing.setTypeface(myTypefaceX);
lblPairing.setTextColor(ColorTool.changeTextColor("#e3e3e5"));
lblPairing.setTextSize(40.0f*screenWidth / 1080);
lblPairing.setText("PAIRING");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//lp.setMargins(xpos, ypos, 0, 0);
setMargins(lblPairing , (int)(0.22*screenWidth) , (int)(0.075*screenHeight) , 0 , 0 );
lblPairing.setLayoutParams(lp);
main.addView(lblPairing , lp);
lblCalibrate.setTypeface(myTypefaceX);
lblCalibrate.setTextColor(ColorTool.changeTextColor("#e3e3e5"));
lblCalibrate.setTextSize(40.0f*screenWidth / 1080);
lblCalibrate.setText("CALIBRATION");
//lp.setMargins(xpos, ypos, 0, 0);
setMargins(lblCalibrate , (int)(0.535*screenWidth) , (int)(0.12*screenHeight) , 0 , 0 );
lblCalibrate.setLayoutParams(lp);
main.addView(lblCalibrate , lp);
//RelativeLayout.setLayoutParams(lp);
/*setLabel(lblPairing , "PAIRING" , (int)(0.22*screenWidth), (int)(0.075*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 40.0f );
setLabel(lblCalibrate , "CALIBRATION" , (int)(0.535*screenWidth), (int)(0.12*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 40.0f );
setLabel(lblFlight1 , "GENERAL" , (int)(0.13*screenWidth), (int)(0.17*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 40.0f );
setLabel(lblFlight2 , "SETTINGS" , (int)(0.195*screenWidth), (int)(0.20*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 40.0f );
setLabel(lblBluetooth , "Bluetooth" , (int)(0.31*screenWidth), (int)(0.795*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 20.0f );
setLabel(lblBLEStatus , "Pending" , (int)(0.33*screenWidth), (int)(0.825*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 20.0f );
setLabel(lblFirmW , "Firmware Version" , (int)(0.34*screenWidth), (int)(0.87*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 20.0f );
setLabel(lblFirmVersion , "0.00" , (int)(0.78*screenWidth), (int)(0.87*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 20.0f );
setLabel(lblnameTitle , "Name" , (int)(0.03*screenWidth), (int)(0.88*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 20.0f );
setLabel(lblnameCustom , "Novax350" , (int)(0.03*screenWidth), (int)(0.93*screenHeight) , ColorTool.changeTextColor("#e3e3e5"),
"fonts/TitilliumText22L003.otf" , screenWidth , mainCt , 20.0f );
*/
iv = (ImageView) findViewById (R.id.image);
if (iv != null) {
iv.setOnTouchListener(this);
}
}
/**
* Respond to the user touching the screen.
* Change images to make things appear and disappear from the screen.
*
*/
public void changeName(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
public static void setMargins (View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}
public void setLabel(TextView textview , String text , int xpos , int ypos, int color,
String fontPath , int screenWidth , Context ctx , float size){
Typeface myTypefaceX = Typeface.createFromAsset(ctx.getAssets(), fontPath ); //"fonts/TitilliumText22L003.otf");
System.out.println("myTypefaceX " + myTypefaceX );
System.out.println("tv " + textview );
textview.setTypeface(myTypefaceX);
textview.setTextColor(color);
textview.setTextSize(size*screenWidth / 1080);
textview.setText(text);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//lp.setMargins(xpos, ypos, 0, 0);
setMargins(textview , xpos , ypos , 0 , 0 );
textview.setLayoutParams(lp);
System.out.println("xpos " + xpos );
System.out.println("ypos " + ypos);
/*RelativeLayout.LayoutParams llp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins(xpos, ypos, 0, 0); // llp.setMargins(left, top, right, bottom);
Typeface myTypefaceX = Typeface.createFromAsset(ctx.getAssets(), fontPath ); //"fonts/TitilliumText22L003.otf");
tv.setTypeface(myTypefaceX);
tv.setTextColor(color);
tv.setTextSize(size*screenWidth / 1080);
tv.setText(text);
tv.setLayoutParams(llp);*/
//main.addView(tv);
}
public boolean onTouch (View v, MotionEvent ev)
{
boolean handledHere = false;
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
int nextImage = -1; // resource id of the next image to display
// If we cannot find the imageView, return.
ImageView imageView = (ImageView) v.findViewById (R.id.image);
if (imageView == null) return false;
// When the action is Down, see if we should show the "pressed" image for the default image.
// We do this when the default image is showing. That condition is detectable by looking at the
// tag of the view. If it is null or contains the resource number of the default image, display the pressed image.
Integer tagNum = (Integer) imageView.getTag ();
int currentResource = (tagNum == null) ? R.drawable.cover_1_d : tagNum.intValue ();
// Now that we know the current resource being displayed we can handle the DOWN and UP events.
switch (action) {
case MotionEvent.ACTION_DOWN :
if (currentResource == R.drawable.cover_1_d) {
nextImage = R.drawable.cover_1_d;
handledHere = true;
/*
} else if (currentResource != R.drawable.cover_1_d) {
nextImage = R.drawable.cover_1_d;
handledHere = true;
*/
} else handledHere = true;
break;
case MotionEvent.ACTION_UP :
// On the UP, we do the click action.
// The hidden image (image_areas) has three different hotspots on it.
// The colors are red, blue, and yellow.
// Use image_areas to determine which region the user touched.
int touchColor = getHotspotColor (R.id.image_areas, evX, evY);
// Compare the touchColor to the expected values. Switch to a different image, depending on what color was touched.
// Note that we use a Color Tool object to test whether the observed color is close enough to the real color to
// count as a match. We do this because colors on the screen do not match the map exactly because of scaling and
// varying pixel density.
ColorTool ct = new ColorTool ();
int tolerance = 25;
nextImage = R.drawable.cover_1_d;
if (ct.closeMatch (Color.RED, touchColor, tolerance)) {
nextImage = R.drawable.cover_1_d;
startActivity(new Intent().setClass(CoverActivity.this, PairingActivity.class).
setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
else if (ct.closeMatch (Color.BLUE, touchColor, tolerance)) {
nextImage = R.drawable.cover_1_d;
startActivity(new Intent().setClass(CoverActivity.this, SettingListHeaderActivity.class).
setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
else if (ct.closeMatch (Color.GREEN, touchColor, tolerance)) {
nextImage = R.drawable.cover_1_d;
startActivity(new Intent().setClass(CoverActivity.this, CalibrateSelectActivity.class).
setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
else if (ct.closeMatch (Color.WHITE, touchColor, tolerance)) nextImage = R.drawable.cover_1_d;
// If the next image is the same as the last image, go back to the default.
// toast ("Current image: " + currentResource + " next: " + nextImage);
if (currentResource == nextImage) {
nextImage = R.drawable.cover_1_d;
}
handledHere = true;
break;
default:
handledHere = false;
} // end switch
if (handledHere) {
if (nextImage > 0) {
imageView.setImageResource (nextImage);
imageView.setTag (nextImage);
}
}
return handledHere;
}
/**
*/
// More methods
/**
* Get the color from the hotspot image at point x-y.
*
*/
public Bitmap generateBM() {
Bitmap result = null;
return result;
}
public int getHotspotColor (int hotspotId, int x, int y) {
ImageView img = (ImageView) findViewById (hotspotId);
if (img == null) {
Log.d ("ImageAreasActivity", "Hot spot image not found");
return 0;
} else {
img.setDrawingCacheEnabled(true);
Bitmap hotspots = Bitmap.createBitmap(img.getDrawingCache());
if (hotspots == null) {
Log.d ("ImageAreasActivity", "Hot spot bitmap was not created");
return 0;
} else {
img.setDrawingCacheEnabled(false);
return hotspots.getPixel(x, y);
}
}
}
/**
* Show a string on the screen via Toast.
*
* @param msg String
* @return void
*/
public void toast (String msg)
{
Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_LONG).show ();
} // end toast
} // end class