简体   繁体   English

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

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

Im beginner in android development. 我是android开发的初学者。 I ve just created a button in main.xml file 我刚在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"
    />

I just give integer value for android: but I got error error: Error: Integer types not allowed 我只给android的整数值:但我得到错误错误:错误:不允许整数类型

How can give fixed width and height for Button here? 如何为Button提供固定的宽度和高度? and What is the main difference between android:layout_width and android:Width? android:layout_width和android:Width之间的主要区别是什么?

Thanks. 谢谢。

To create a button with fixed height and width, you can give values in both px or in dp. 要创建具有固定高度和宽度的按钮,您可以在px或dp中给出值。

giving values in dp is more convenient , bec android automatically scale the height and width in ldpi,mdpi and hdpi devices. 在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 android:layout_width和android:width有什么区别?

try it like this 试试这样

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

where dip is density indepedent pixel 其中dip是密度独立像素

You need to give Numeric Values as following, 您需要提供如下数值,

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

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

android:layout_width="25dp"

Why you want to do it . 你为什么要这样做。 better use wrap content, or use weight tag, so it will support and will look good on all sizes devices. 更好地使用包装内容,或使用重量标签,因此它将支持并在所有尺寸的设备上看起来都很好。

You need To give Like this 你需要这样做

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

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

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