如何在安卓中为按钮指定固定的宽度和高度。

3

我是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:传递整数值,但是出现了错误:error: Error: Integer types not allowed(不允许整数类型)

在这里如何为按钮(Button)指定固定的宽度和高度?另外,android:layout_width 和 android:Width 有什么主要区别?

谢谢。


了解这些字段实际上意味着什么。 - JoxTraex
5个回答

13

要创建一个具有固定高度和宽度的按钮,您可以在像素(px)和设备独立像素(dp)中都赋值。

使用dp赋值更加方便,因为Android会自动在低密度(ldpi)、中密度(mdpi)和高密度(hdpi)设备上缩放高度和宽度。

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

android:layout_width 和 android:width 有什么区别?


1

试着这样做

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

dip 是指密度无关像素。


请参考以下链接以了解更多关于Android中px、dp、dip和sp的区别:https://dev59.com/A3I-5IYBdhLWcg3wEELu - vipin

1

您需要按照以下方式提供数字值:

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

1
使用整数(25,40...) + 类型(DP、DIP、PX):就像这样。
android:layout_width="25dp"

为什么要这样做。最好使用wrap content或使用weight标签,这样它将支持并且在所有尺寸的设备上看起来很好。


0

你需要像这样给出

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接