Home > Blockchain >  Display elements in the same place for all devices
Display elements in the same place for all devices

Time:05-14

I'm trying move items bottom middle, and I want that it was on bottom middle for all devices. And I need add text above and under my imagviews on middel. Sorry for trivial question.

This is my code in Android Studio.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="16dp">
    
    <ImageView
        android:id="@ id/img_instagram"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_instagram" />

    <ImageView
        android:id="@ id/img_vk"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_centerInParent="true"
        android:layout_marginStart="8dp"
        android:src="@drawable/ic_vk_crug" />

    <ImageView
        android:id="@ id/gmail"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_centerInParent="true"
        android:layout_marginStart="8dp"
        android:src="@drawable/ic_icons8_gmail" />

</LinearLayout>

CodePudding user response:

try to set two gravitys with

android:gravity="center_horizontal|bottom"

btw. layout_centerInParent is RelativeLayout params, is no-op in your code, you may safely remove all occurences

Adding a text is more complex, as you are using LinearLayout as root. You should switch to RelativeLayout or ConstraintLayout

CodePudding user response:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|bottom"
        android:padding="16dp">
    
        <LinearLayout
            android:id="@ id/llInstagram"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">
    
            <TextView
                android:id="@ id/tvInstagram"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Instagram">
    
            </TextView>
    
            <ImageView
                android:id="@ id/img_instagram"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@ id/llVK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@ id/llInstagram"
            android:orientation="vertical">
    
            <TextView
                android:id="@ id/tv_vk"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="VK">
    
            </TextView>
    
            <ImageView
                android:id="@ id/img_vk"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginStart="8dp"
                android:src="@mipmap/ic_launcher" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@ id/llVK"
            android:orientation="vertical">
    
            <TextView
                android:id="@ id/tvgmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Gmail">
    
            </TextView>
    
            <ImageView
                android:id="@ id/gmail"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginStart="8dp"
                android:src="@mipmap/ic_launcher" />
    
        </LinearLayout>
    
    </RelativeLayout>
  • Related