解析Android中如何布局定位Widget控件

移动开发
Android中如何布局定位Widget控件是本文要介绍的内容,主要是来了解并学习Android Widget控件的应用,具体内容的实现来看本文详解。

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控件内容的学习能对你有所帮助,

责任编辑:zhaolei 来源: 互联网
相关推荐

2010-07-13 09:08:27

Widget开发

2011-09-07 13:30:48

Android WidTabWidget

2011-09-07 14:01:41

Android Wid实例

2011-09-09 10:00:20

Android Wid开发

2011-09-07 17:54:40

Android Wid开发

2011-09-07 14:39:47

Android Wid设计

2011-09-08 15:40:45

Android Wid组件

2011-09-07 14:25:53

Android Wid设计

2010-09-13 13:12:57

CSS定位

2010-04-23 11:21:05

Widget开发

2011-09-09 17:59:26

QT Widget

2011-09-07 14:55:28

Android WidAppWidget事件

2011-09-08 16:17:45

Widget

2011-09-08 14:21:37

jQueryWidget

2011-09-09 19:23:52

Widget

2010-09-14 16:57:29

DIV绝对定位CSS

2011-09-09 13:23:17

Widget

2011-09-08 14:05:19

WidgetFlexViewer

2011-04-07 08:59:47

Android交互设计

2011-09-07 14:20:42

Android Wid组件
点赞
收藏

51CTO技术栈公众号