如何为Android中的Button提供固定的宽度和高度

时间:2012-02-23 06:24:17

标签: android android-layout

我是android开发的初学者。我刚刚在main.xml文件中创建了一个按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >



  <Button 
    android:text="Click"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

我只为android提供整数值:但是我收到了错误 错误:错误:不允许整数类型

如何为Button提供固定的宽度和高度? 和 android:layout_width和android:Width之间的主要区别是什么?

感谢。

5 个答案:

答案 0 :(得分:11)

要创建一个固定高度和宽度的按钮,您可以在px或dp中给出值。

在dp中给出值更方便,因为android会自动缩放ldpi,mdpi和hdpi设备中的高度和宽度。

<Button 
    android:text="Click"
    android:layout_width="100dp"
    android:layout_height="50dp"
    />

What is the difference between android:layout_width and android:width

答案 1 :(得分:1)

尝试这样

<Button 
    android:text="Click"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    />

其中dip是与密度无关的像素

答案 2 :(得分:1)

您需要提供以下数字值,

 <Button 
    android:text="Click"
    android:layout_width="250dp"
    android:layout_height="50dp" />

答案 3 :(得分:1)

使用整数(25,40 ...)+类型(DP,DIP,PX):喜欢

android:layout_width="25dp"

你为什么要这样做。更好地使用包装内容,或使用重量标签,因此它将支持并且在所有尺寸的设备上看起来都很好。

答案 4 :(得分:0)

你需要像这样给予

<Button android:layout_Width="150dp"
    android:layout_Height="50dp"  />