你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发教程:取得布局中指定控件的宽高

[日期:2013-02-17] 来源:Linux社区  作者:zlqqhs [字体:大 中 小]

Android开发教程:取得布局中指定控件的宽高

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class GetWH extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        View view = getLayoutInflater().inflate(R.layout.main, null);
        Button button = (Button) view.findViewById(R.id.button2);
        int width = button.getLayoutParams().width;
        int height = button.getLayoutParams().height;
        Log.i("System.out", width + "-----" +  height);
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/button2"
        android:layout_width="80px"
        android:layout_height="80dp"
        android:text="Button" />
</LinearLayout>

从结果中大家可以看出些什么呢?大笑

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/4/23 11:04:36
你确定能得到控件宽高?