简体   繁体   中英

How to give Fixed width and height for Button in Android

Im beginner in android development. I ve just created a button in main.xml file

<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

How can give fixed width and height for Button here? and What is the main difference between android:layout_width and android:Width?

Thanks.

To create a button with fixed height and width, you can give values in both px or in dp.

giving values in dp is more convenient , bec android automatically scale the height and width in ldpi,mdpi and hdpi devices.

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

What is the difference between android:layout_width and 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

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

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"  />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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