【问题标题】:Android Buttons not scalingAndroid按钮不缩放
【发布时间】:2012-10-05 01:36:25
【问题描述】:

这似乎是一个非常基本的问题,但我似乎无法弄清楚。我已经阅读了大量关于如何使 android 可扩展到大小屏幕的文章。

我有一个专为平板电脑设计的应用程序,在该应用程序上运行良好。但是,当我在开发手机(手机)版本时,我想确保如果有人在手机上打开它,它至少看起来“还不错”。

除了按钮之外,每个人的缩放都很棒......我似乎无法让按钮缩放。

它们在选项卡上看起来很棒......但在手机上,它们占据了整个屏幕。

感谢您的帮助。

代码如下:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_landscape"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_menu_plus_landscape"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="38dp"
        android:layout_marginTop="200dp"
        android:background="@layout/button_main_selector"
        android:padding="40dp"
        android:text="@string/main_start_list"
        android:textColor="@color/white"
        android:textSize="30dp"
        android:typeface="sans"/>


    <Button
        android:id="@+id/button_emulator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adview"
        android:layout_alignLeft="@+id/button_list"
        android:layout_alignRight="@+id/button_list"
        android:layout_marginBottom="168dp"
        android:background="@layout/button_main_selector"
        android:padding="40dp"
        android:text="@string/main_start_emulator"
        android:textColor="@color/white"
        android:textSize="30dp"
        android:typeface="sans" />

</RelativeLayout>

布局/button_main_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Focused-->
    <item   android:state_focused="true"
            android:state_pressed="false"
            android:drawable="@drawable/main_buttons_selected"
            />
<!-- Button Focused Pressed-->
    <item   android:state_focused="true"
            android:state_pressed="true"
             android:drawable="@drawable/main_buttons_selected"
            />
<!-- Button Pressed-->
    <item   android:state_focused="false"
            android:state_pressed="true"
            android:drawable="@drawable/main_buttons_selected"
            />
<!-- Button Default Image-->
    <item   android:drawable="@drawable/main_buttons"/>

</selector>

drawable-hdpi/main_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
 <solid android:color="#71bbe3"/> 
    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
      android:topLeftRadius="10dp"
      android:topRightRadius="10dp"/>
</shape>

我也尝试将我的按钮文件放入“可绘制”中,但这似乎没有帮助。有什么建议? (注意:我的按钮只是纯色,没有图像)

res/drawable/main_buttons.xml

res/drawable/main_buttons_selected.xml

res/drawable/button_main_selector.xml

【问题讨论】:

  • 你的按钮背景有不同的尺寸吗?
  • 我只用纯色,没有图片。
  • 嗨,你能给出你对这个文件的完整设计,这样我就可以追踪到哪里出了问题?因为这是相对布局,你提供的代码中不存在@+id/adview。
  • 很抱歉,它只是页面底部的另一个小视图。它与问题无关,所以我不想弄乱我的问题。您可以删除 layout_above 并说 layout_alightParentBottom = "true"。我并不像缩放问题那样关心位置。

标签: android android-layout android-xml android-button


【解决方案1】:

【讨论】:

  • 这是为了使用图像作为背景。我不使用任何图像,只使用纯色。
【解决方案2】:

根据您提供的信息,我预测问题是,您只提供了 Hdpi 中的按钮。因此,当您在手机中运行应用程序时,按钮取自 hdpi,这会使手机看起来很奇怪。这可以通过在drawable或ldpi中给出按钮来解决。它将适合电话屏幕。这可能是您的问题。

【讨论】:

  • 我应该在drawable中添加什么,选择器?我试过这样可以防止我给按钮充气?或者,如果我将 main_button 和 main_button_selected 放入可绘制对象中,则没有任何变化……而且它们只是纯色。
【解决方案3】:

您只需将两个 xml 都放入 drawable

通过在项目的 res 中创建一个名为 drawable 的文件夹

res/drawable/main_buttons.xml

res/drawable/button_main_selector.xml

【讨论】:

  • 如果我将 button_main_selector 从 layout 放到 drawable 我的按钮无法加载。当我将其他两个: main_buttons 和 main_buttons_selected 放入 drawable 中时......没有任何变化。
【解决方案4】:

我看过你的代码,首先我会告诉你,你应该发布你想要什么样的视图的图像,第二个是你给出的

android:layout_marginRight="38dp"

android:layout_marginTop="200dp"

在你的第一个按钮代码和你的第二个按钮中,你已经给了这样的边距

android:layout_marginBottom="168dp"

其他的一切都取决于你想要实现什么样的设计。

我对你的建议是永远不要特别从左到右给予边距。因为例如,如果你从左边到 50px 的边距在普通设备中看起来还不错,而在高分辨率设备中看起来不太好,因为它需要 50px在任何分辨率 device.instead 使用这种语法 android:alignParentLeft=true 并始终习惯在您的相对布局中使用这种语法。使用这种语法,您的元素将始终位于您想要的位置..转到android开发者网站并检查线性布局和相对布局之间的区别以及线性和相对布局的不同属性研究所有属性,如fill_parentwrap_contentmatch_parent的含义比100%你将得到解决方案。如果您有任何疑问而不是在这里加入我 https://chat.stackoverflow.com/rooms/13436/smart-phones-developer 您可以在这里问我您的问题。我会建议您发布您的图片

谢谢 阿米尔汗一世。

【讨论】:

  • 好建议。但这并没有解决我当前的问题。稍后我会做更多的阅读,因为我可能对相对布局有一些误解。但是我的按钮应该可以缩放。
猜你喜欢
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多