Android:当我在不同手机上查看应用时按钮更改位置

时间:2013-11-25 12:49:39

标签: android button layout

我有一个带有四个按钮的活动,当我在模拟器上播放时一切正常,但是当我在我的手机上测试它时按钮变大或实际上背景或屏幕变小但按钮保持不变,所以它看起来更大。

我可以按照与活动家背景相同的方式使按钮适应屏幕吗?

这是我的xml文件

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainn"
android:gravity="top" >

<Button
    android:id="@+id/b_labor"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="114dp"
     android:background="@drawable/labb" />

<Button
    android:id="@+id/b_mosul"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/b_labor"
    android:layout_alignLeft="@+id/b_labor"
    android:layout_marginBottom="32dp"
     android:background="@drawable/ninaa" />




<Button
    android:id="@+id/b_trafic"
    style="@style/Theme.Transparent"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/b_mosul"
    android:layout_alignLeft="@+id/b_mosul"
    android:layout_marginBottom="28dp"
     android:background="@drawable/traficc" />


<Button
    android:id="@+id/b_nati"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/b_trafic"
    android:layout_alignLeft="@+id/b_trafic"
    android:layout_marginBottom="34dp"
    android:background="@drawable/natiii"

     />

2 个答案:

答案 0 :(得分:0)

由于我们都知道Android设备的屏幕尺寸不同,我们应该检查不同设备中按钮的对齐方式,开始查看此链接Developer.google

有stackoverflow答案,这将有助于你  screen size vs button position

答案 1 :(得分:0)

我认为您是Android的新手,因此您需要在Android设备上创建UI元素之前学习一些内容。 UI创建起来并不简单,您应该了解不同的概念,以便在Android中创建复杂的GUI。上述情况下的主要问题是每个设备都有不同的分辨率,如果您对按钮进行硬编码,则它将无法根据您的设备分辨率和屏幕尺寸进行调整。因此,在创建任何UI元素之前,您需要记住一些要点:

  1. 永远不要尝试对任何特殊的GUI参数进行硬编码,因为这些参数会根据设备而改变。

  2. 始终只从UIThread创建和更新GUI。

  3. 使用异步任务,服务和线程来执行繁重的任务和后台任务。

  4. 始终尝试以最小负载保持UI线程。

  5. 要了解有关GUI特性的更多信息,请尝试探索不同布局和视图的LayoutParams。

  6. 还有很多东西需要学习。根据我的创建GUI,你应该从相对布局开始,尽量不要使用绝对布局。

    您可以查看以下链接以获取一些帮助:

    https://stackoverflow.com/questions/2992754/ui-design-tips-and-tutorials-for-android

    http://mobile.smashingmagazine.com/2012/07/26/android-design-tips/