Android Studio预览中的项目显示较大?

时间:2015-06-18 04:29:24

标签: android preview

所以,我在Android Studio中设计我的观点,这种情况发生了:

http://puu.sh/it8CF/aaa697a7d2.jpg< - 它在工作室预览中的外观

http://puu.sh/it8Gk/047a18f276.jpg< - 它在仿真器视图中的外观,用于预览的完全相同的设备

这使我无法为我的应用设计UI。这些图标是有意的,但它们重叠并非完全不符合设计。它们应该是彼此相邻的...我的朋友正在从他的计算机运行完全相同的模拟器/项目,它在Android Studio预览中看起来像这样。

以下是他的Android预览版的截图:

http://puu.sh/it8OZ/4647e7a966.jpg

那么我从哪里开始,有关如何修复的想法?

如下所述,这是pastebin中的XML版本:http://pastebin.com/QxfBy6F9

相对布局中总共有20个图标,但这种情况发生在所有不同的布局中,而不仅仅是相对布局。我尝试将其编码为线性布局,每行有4个图标,但是由于尺寸的原因,如果更改将布局3,然后将最后一个图标的75%切掉,即使在手机上也是如此它最多可以显示5个。

1 个答案:

答案 0 :(得分:1)

您可以尝试一些解决方案。

<强> 1。保持您的布局不变,并使用以下代码设置图像按钮的宽度,高度尺寸:

int screenWidth = ...; //Calculate your screen width
imageButton1.getLayoutParams().width = screenWidth / 5;
imageButton1.getLayoutParams().height = screenWidth / 5;

您可以使用以下代码计算屏幕宽度:

public static int getScreenWidth(Context context) {
   int width;
        
   if (android.os.Build.VERSION.SDK_INT >= 13) {
     WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
     Display display = wm.getDefaultDisplay();
     Point size = new Point();
     display.getSize(size);
     width = size.x;
   } else {
     WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
     Display display = wm.getDefaultDisplay();
     width = display.getWidth();  // deprecated
   }

   return width;
}

<强> 2。将您的布​​局改为每行的LinearLayout:

<?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:orientation="vertical"
   android:background="#00BCD4">
 
  
   <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
     <ImageButton
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       />
     <ImageButton
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       />
     <ImageButton
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       />
     <ImageButton
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       />
   </LinearLayout>
   <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
     <ImageButton
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       />
     <ImageButton
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       />
     <ImageButton
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       />
     <ImageButton
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       />
   </LinearLayout>
   
   // Add more LinearLayout here...
  
  
</LinearLayout>