歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:取得布局中指定控件的寬高

Android開發教程:取得布局中指定控件的寬高

日期:2017/3/1 10:01:37   编辑:Linux編程

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

Copyright © Linux教程網 All Rights Reserved