Android Center Buttons

How to center the button and make it so that it is 10 pixels on each side. Mostly 100% wide minus 10 pixels left and right.

+3
source share
3 answers

try the following:

layout_width="fill_parent"
layout_marginLeft="10dip"
layout_marginRight="10dip"
+12
source

You can set the maragin to the left and the mariag to the right. If you used Relative Layout, you can use the following parameters layout_centerInParent, android: layout_centerVertical, android: layout_centerHorizontal

If you want to center the button vertically, use Center vertical true, or if you want the center button in a horizontal position, use Center Horizontal true as shown below

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="SignUp Here"
    android:id="@+id/mysignup"
    android:layout_alignBottom="@+id/mylogin"
    android:layout_weight="1"
    android:layout_centerInParent="true" <!--  use depending upon your need -->
    android:layout_centerVertical="true" <!--   use depending upon your need -->
    android:layout_centerHorizontal="true" <!--  use depending upon your need -->
/>

Dip Density Indepent Pixels, , 10, , , px () , , , , dip dp , dp dip

+8

First: forget about the pixel, always use dp as the unit. Do you want to add it programmatically or through layouts of xml files?

If you need to add it programmatically, use this:

LinearLayout layout = new LinearLayout(context);
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.FILL_PARENT));

    Button button = new Button(context);
    button.setText("Some label");
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.FILL_PARENT, 
            1);
    params.setMargins(10, 0, 10, 0);
    button.setLayoutParams(params);

    layout.addView(button);

If you want to add it from the layout file, do the following:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:paddingTop="3dp"
android:paddingRight="10dp" android:paddingLeft="10dp"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<Button android:layout_height="fill_parent" android:id="@+id/scroll_story_title"
    android:ellipsize="end" android:layout_gravity="center"
    android:maxLines="2" android:gravity="center"
    android:text="Something to show to the user and that pretty cool"
    android:layout_marginTop="3dp" android:textSize="11sp"
    android:layout_width="fill_parent"></Button>

+3
source

Source: https://habr.com/ru/post/1792784/


All Articles