【问题标题】:Items appear larger in Android Studio preview?项目在 Android Studio 预览中显得更大?
【发布时间】:2015-06-18 12:53:01
【问题描述】:

所以我在 Android Studio 中设计我的视图并且发生了这种情况:

http://puu.sh/it8CF/aaa697a7d2.jpg

http://puu.sh/it8Gk/047a18f276.jpg

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

这是他的安卓预览截图:

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

那么我该从哪里开始,有什么解决方法的想法吗?

下面要求的是 pastebin 中的 XML 版本:http://pastebin.com/QxfBy6F9

相对布局中总共有 20 个图标,但是这发生在所有不同的布局中,而不仅仅是相对的。我正在尝试将其编码为线性布局,每行有 4 个图标,但是由于大小的布局,如果更改会放置 3,然后将最后一个图标的 75% 关闭,即使在电话上它可以最多显示 5 个。

【问题讨论】:

  • 你能发布你的xml布局吗?
  • pastebin.com/QxfBy6F9 这是它的pastebin。它目前是一个相对布局。试图使其成为线性布局,但直到我可以正确加载视图之前它不会工作。编辑:每个布局都发生这种情况,而不仅仅是这个。
  • 检查更新的答案,让我知道它是否有效。

标签: android preview


【解决方案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>

【讨论】:

  • 看问题是,问题不在于它在我的手机上显示不正确,它只是在 Android Studio 中显示不正确。所以现在当我尝试将它切换到我想要的线性布局时,就会发生这种情况。 puu.sh/itacp/4a6048ce4f.jpg 只能在设计预览中断前放入 3 个图标。
  • 是的,但这也会使它们在手机屏幕上变小。问题是,预览模式会炸毁所有图标并使它们比实际更大。因此,如果在预览模式下调整所有图标的大小,使它们都适合,当它显示在屏幕上时,它会比预期的要小得多。这是一个 android studio 预览渲染问题。
  • 是的,因为模拟器的大小比手机大:)
  • 虽然我的朋友模拟器显示正确的大小,而不需要调整每个图标的大小,但这没有意义。
  • 如果您的应用在不同的手机上运行,​​我认为我们应该调整视图大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
相关资源
最近更新 更多