繁体   English   中英

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

[英]How to give Fixed width and height for Button in Android

我是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之间的主要区别是什么?

谢谢。

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

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

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

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

试试这样

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

其中dip是密度独立像素

您需要提供如下数值,

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

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

android:layout_width="25dp"

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

你需要这样做

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM