Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不同Android屏幕大小上的按钮大小_Android_Android Layout - Fatal编程技术网

不同Android屏幕大小上的按钮大小

不同Android屏幕大小上的按钮大小,android,android-layout,Android,Android Layout,我有一个简单的布局,它只有两个小部件:一个全屏ImageView作为背景,一个按钮。背景没有问题,但是,我无法定位和调整按钮大小以适应所有屏幕。按钮应该位于屏幕的2/3处,居中对齐 这是我的活动\u login.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=

我有一个简单的布局,它只有两个小部件:一个全屏
ImageView
作为背景,一个
按钮
。背景没有问题,但是,我无法定位和调整按钮大小以适应所有屏幕。按钮应该位于屏幕的2/3处,居中对齐

这是我的
活动\u login.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="hk.goodsight.kitchenlifestyle.LoginActivity">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/bg_signin"
        android:contentDescription="background"
        android:scaleType="centerCrop"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="382dp"
        android:layout_height="66dp"
        android:text="@string/sign_in"
        android:id="@+id/btnLogin"
        android:background="@drawable/btn_signin"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="150dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white" />
</RelativeLayout>

根据其他SO问题,一些人建议如下:

  • 将px转换为dp,以计算按钮的大小和位置
  • 对不同的屏幕大小使用不同的布局
  • 我应该选择哪一个


    p、 美国必须支持安卓4.0,因此我不能使用API 23中引入的百分比作为利润下限。

    我想这是一个有点幼稚的方法,但我已经在许多设备上测试了这个预览,它显示了你想要的。我在父布局上设置了图像背景,而不是
    ImageView
    。用原始图像资源替换该值

    <?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:background="@mipmap/ic_launcher"
    android:orientation="vertical">
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"/>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="blah"
            android:textColor="@android:color/white" />
    </LinearLayout>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"/>
    
    
    

    我想这是一个有点幼稚的方法,但我已经在许多设备上测试了这个预览,它显示了您想要的。我在父布局上设置了图像背景,而不是
    ImageView
    。用原始图像资源替换该值

    <?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:background="@mipmap/ic_launcher"
    android:orientation="vertical">
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"/>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="blah"
            android:textColor="@android:color/white" />
    </LinearLayout>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"/>
    
    
    

    您可以从WindowManager获取显示大小,然后通过编程将按钮边距顶部设置为其2/3的分数,例如:

    android.view.Display display = ((android.view.WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Button btn = new Button(this);
        LayoutParams params = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );
        params.setMargins(0,(int)(display.getHeight()*0.666),0,0);
        btn.setLayoutParams(params);
    

    您可以从WindowManager获取显示大小,然后通过编程将按钮边距顶部设置为其2/3的分数,类似于:

    android.view.Display display = ((android.view.WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Button btn = new Button(this);
        LayoutParams params = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );
        params.setMargins(0,(int)(display.getHeight()*0.666),0,0);
        btn.setLayoutParams(params);
    

    您是否尝试过使用布局权重?是的,尝试过
    layout\u weight
    。由于没有其他元素,
    layout\u weight
    无法计算正确的位置。下面发布了一个解决方案。是否尝试使用layout\u weight?是的,尝试了
    layout\u weight
    。由于没有其他元素,
    layout\u weight
    无法计算正确的位置。下面发布了一个解决方案。您暗示了我。我最终使用了
    空格
    而不是逻辑更简单的
    线性布局
    。很高兴知道你已经实现了你想要的:)你暗示了我。我最终使用了
    空间
    而不是逻辑更简单的
    线性布局
    。很高兴知道你已经实现了你想要的:)