【问题标题】:Layout issue in Android Studio for large and normal screensAndroid Studio 中大屏幕和普通屏幕的布局问题
【发布时间】:2016-12-21 05:07:06
【问题描述】:

在我的 res 文件夹中:layout 文件夹中的 activity_main.xml(因此是默认文件夹),layout-large 文件夹和 layout-normal 文件夹中的相同文件。

我在三星 Galaxy S5(大屏幕)的预览中设置了我想要的布局,然后运行 ​​AVD:这里的布局不同。有一个图像位置不同。相反,当我在普通屏幕上运行应用程序时,不会发生这种情况。

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

【问题讨论】:

    标签: java android android-studio layout screen


    【解决方案1】:

    在您的 java 文件中:

    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);
    

    ##对所有视图使用上述方法##

    创建方法

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多