Android Java按钮坐标

Android Java按钮坐标,java,android,xml,android-layout,button,Java,Android,Xml,Android Layout,Button,如果屏幕很大,我想调整按钮的大小。所以我用这个方法做了。我得到了屏幕大小并将按钮宽度设置为screenWidth/4。但现在我不能把按钮放在正确的位置。我使用了相对布局,并从xml中放置了它们的位置,但当我更改它们的大小时,它们彼此位于0,0位置。 这里是XML <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android

如果屏幕很大,我想调整按钮的大小。所以我用这个方法做了。我得到了屏幕大小并将按钮宽度设置为screenWidth/4。但现在我不能把按钮放在正确的位置。我使用了相对布局,并从xml中放置了它们的位置,但当我更改它们的大小时,它们彼此位于0,0位置。

这里是XML

<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"
    android:background="@color/pink"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.vahe_muradyan.yourquote.MainActivity" >

    <Button
        android:id="@+id/openCamera"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:background="@drawable/roundbuttom"
        android:text="@string/camera" />

    <Button
        android:id="@+id/openGallery"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignLeft="@+id/openDefaults"
        android:layout_below="@+id/openDefaults"
        android:layout_marginTop="43dp"
        android:background="@drawable/roundbuttom"
        android:text="@string/gallery" />

    <Button
        android:id="@+id/openDefaults"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignLeft="@+id/openCamera"
        android:layout_centerVertical="true"
        android:background="@drawable/roundbuttom"
        android:text="@string/defaults" />

</RelativeLayout>


我在Nexus S和Nexus 10上加载了图像。

根据屏幕大小添加尺寸

您需要在res文件夹中创建名为的新文件夹

值-sw600dp

在normal values文件夹中创建一个名为

dimens.xml

在该文件中写入

<dimen name="button_width">70dp</dimen>
<dimen name="button_height>70dp</dimen>
<dimen name="button_text_size">14sp</dimen>
70dp
140dp
20便士
//选择合适的尺码

在xml文件中写入以下内容:

<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"
    android:background="@color/pink"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.vahe_muradyan.yourquote.MainActivity" >

    <Button
        android:id="@+id/openCamera"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:background="@drawable/roundbuttom"
        android:textSize="@dimen/button_text_size"
        android:text="@string/camera" />

    <Button
        android:id="@+id/openGallery"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:textSize="@dimen/button_text_size"
        android:layout_alignLeft="@+id/openDefaults"
        android:layout_below="@+id/openDefaults"
        android:layout_marginTop="43dp"
        android:background="@drawable/roundbuttom"
        android:text="@string/gallery" />

    <Button
        android:id="@+id/openDefaults"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:textSize="@dimen/button_text_size"
        android:layout_alignLeft="@+id/openCamera"
        android:layout_centerVertical="true"
        android:background="@drawable/roundbuttom"
        android:text="@string/defaults" />

</RelativeLayout>

系统将根据运行应用程序的设备选择正确的值

按钮将足够大(如果您在values-sw600dp/dimens.xml文件中提供了良好的值)

这是文件夹树:


玩得开心

使您的按钮与父按钮匹配,为什么需要调整它们的大小?这不是iphone-android有很好的调整大小技术,包括相对布局和线性布局。你能发布你的按钮的xml吗?如果我给它们匹配的父项,它们会变大。你如何设置它们的位置?您希望布局是什么样的?包装内容如何?包装内容在10英寸上会很小,我会尝试一下,但为什么文件夹名称是sw600dp?7英寸平板电脑10英寸平板电脑,2013年年中生产的设备和2014年生产的设备。(2014年的设备大多是xhdpi和xxhdpi,即使它们是手机大小的)但是,如果您不创建sw720dp文件夹,将使用您的sw600dp。术语sw600dp表示屏幕宽度**至少**600dp,因此sw720dp表示“至少720dp屏幕宽度”
<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"
    android:background="@color/pink"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.vahe_muradyan.yourquote.MainActivity" >

    <Button
        android:id="@+id/openCamera"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:background="@drawable/roundbuttom"
        android:textSize="@dimen/button_text_size"
        android:text="@string/camera" />

    <Button
        android:id="@+id/openGallery"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:textSize="@dimen/button_text_size"
        android:layout_alignLeft="@+id/openDefaults"
        android:layout_below="@+id/openDefaults"
        android:layout_marginTop="43dp"
        android:background="@drawable/roundbuttom"
        android:text="@string/gallery" />

    <Button
        android:id="@+id/openDefaults"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:textSize="@dimen/button_text_size"
        android:layout_alignLeft="@+id/openCamera"
        android:layout_centerVertical="true"
        android:background="@drawable/roundbuttom"
        android:text="@string/defaults" />

</RelativeLayout>