/ / Layout-Problem in Android Studio für große und normale Bildschirme - Java, Android, Android-Studio, Layout, Bildschirm

Layout-Problem in Android Studio für große und normale Bildschirme - Java, Android, Android-Studio, Layout, Bildschirm

In meinem res-Ordner habe ich: activity_main.xml im Layout-Ordner (also der Standard-Ordner), die gleiche Datei im layout-large-Ordner und im Layout-normalen Ordner.

Ich stelle das Layout in der Vorschau auf einSamsung Galaxy S5 (also ein großer Bildschirm) und dann starte ich den AVD: Hier ist das Layout anders. Da ist ein Bild anders positioniert. Im Gegensatz dazu passiert das nicht, wenn ich die App auf einem normalen Bildschirm laufe.

Here"s the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/sfondo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:scaleType="fitXY"
android:layout_marginTop="-150dp" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_centerHorizontal="true"
android:src="@drawable/tronco1"
android:layout_alignTop="@+id/imageView" />

<ImageView
android:layout_width="370dp"
android:layout_height="230dp"
android:id="@+id/imageView3"
android:src="@drawable/titolo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<ImageButton
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageButton"
android:src="@drawable/play_h"
android:scaleType="fitCenter"
android:background="#00ffffff"
android:layout_marginTop="40dp"
android:layout_below="@+id/imageView3"
android:layout_centerHorizontal="true" />

<ImageButton
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageButton3"
android:src="@drawable/options_h"
android:scaleType="fitCenter"
android:background="#00ffffff"
android:layout_marginTop="16dp"
android:layout_below="@+id/imageButton2"
android:layout_alignLeft="@+id/imageButton"
android:layout_alignStart="@+id/imageButton" />

<ImageButton
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageButton2"
android:src="@drawable/score_h"
android:scaleType="fitCenter"
android:background="#00ffffff"
android:layout_marginTop="17dp"
android:layout_below="@+id/imageButton"
android:layout_alignLeft="@+id/imageButton"
android:layout_alignStart="@+id/imageButton" />

<ImageButton
android:layout_width="160dp"
android:layout_height="40dp"
android:id="@+id/imageButton4"
android:src="@drawable/removeads_h"
android:scaleType="fitCenter"
android:background="#00ffffff"
android:layout_below="@+id/imageButton3"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp" />

Antworten:

0 für die Antwort № 1

in deiner Java-Datei:

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowmanager = (WindowManager)  getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
windowmanager.getDefaultDisplay().getMetrics(displayMetrics);


int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;

FrameLayout.LayoutParams frm = (FrameLayout.LayoutParams)findViewById(R.id.trump_center_frame).getLayoutParams();


frm.width = get_screen_width(710);
frm.height = get_screen_height(270);
frm.bottomMargin = get_screen_height(80);
frm.gravity = Gravity.CENTER;


TextView title = (TextView) findViewById(R.id.trump_title);

frm = (FrameLayout.LayoutParams)title.getLayoutParams();//here title is in frame layout
frm.gravity = Gravity.CENTER_HORIZONTAL;
frm.topMargin = get_screen_height(24);

## verwende die oben genannte Methode für alle View ##

Methode erstellen

private int get_screen_width(int w) {
return deviceWidth * w / 720;// dimention(720)(width) of screen in which you set layout
}

private int get_screen_height(int h) {
return deviceHeight * h / 1280;// dimention(1280)(height) of screen in which you set layout
}