Android中如何布局定位Widget控件是本文要介绍的内容,主要是来了解并学习Android Widget控件的应用,具体内容的实现来看本文详解。
<?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:id="@+id/laymain"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:id="@+id/id_hello"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
然后我想在水平坐标居中, 纵坐标为手机屏幕 3 /5 的位置显示这个LinearLayout里的TextView, 有什么比较
简单的方法呢?
做过网页的朋友一定很想利用MarginLeft, MarginTop属性了, 没错! 获取TextView的MarginLayoutParams是关键!
贴出代码:
package com.dengsi.android;
import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class fixposition extends Activity {
private LinearLayout laymain = null;
private TextView textview_ = null;
int sWidth_ = 0;
int sHeight = 0;
Paint fontPaint_ = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview_ = (TextView) findViewById(R.id.id_hello);
laymain = (LinearLayout)findViewById(R.id.laymain);
TextView spaceTV = new TextView(this);
fontPaint_ = new Paint();
Display disp_ = this.getWindowManager().getDefaultDisplay();
sWidth_ = disp_.getWidth();
sHeight = disp_.getHeight();
// TODO Auto-generated method stub
int toY = (sHeight * 3) / 5;
int toX = (sWidth_ - (int) fontPaint_.measureText(textview_.getText()
.toString())) >> 1;
System.out.println("x = " + toX + ", y = " + toY);
laymain.addView(spaceTV, 0);
spaceTV.setLayoutParams(new LinearLayout.LayoutParams(0, toY));
ViewGroup.MarginLayoutParams mlp = (MarginLayoutParams) textview_.getLayoutParams();
mlp.leftMargin = toX;
textview_.setLayoutParams(mlp);
textview_.requestLayout();
textview_.invalidate();
laymain.invalidate();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
模拟器显示效果如图(然后可以把这个放在欢迎画面, 继续做滚动字幕等扩展, 我这里没有用onDraw方法):
小结:解析Android中如何布局定位Widget控件的内容介绍完了,希望通过本文的学习能Android Widget控件内容的学习能对你有所帮助,