布局元素覆盖角落

时间:2016-12-01 12:56:39

标签: android xml absolutelayout

我正在使用AbsoluteLayout尝试用我想要添加的元素覆盖整个屏幕以创建“FullScreen”菜单,但它不会覆盖它。这就是我得到的。让我分享我的代码,因为你看到粉红色面板没有覆盖屏幕的角落为什么会这样? enter image description here

MainActivity xml

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout 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: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.example.apos.champquess.MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="77dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ImageView
    android:layout_width="404dp"
    android:layout_height="550dp"
    android:id="@+id/imageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/button"
    android:layout_alignEnd="@+id/button"
    android:src="@color/colorAccent"
    android:layout_below="@+id/button"
    android:layout_x="280dp"
    android:layout_y="78dp" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton"
    android:id="@+id/radioButton"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="231dp"
    android:layout_marginStart="231dp"
    android:layout_x="150dp"
    android:layout_y="73dp" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New CheckBox"
    android:id="@+id/checkBox"
    android:layout_x="710dp"
    android:layout_y="238dp" />

和MainActity Java

package com.example.apos.champquess;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //set content view AFTER ABOVE sequence (to avoid crash)
        this.setContentView(R.layout.activity_main);
        setContentView(R.layout.activity_main);
    }
}

编辑:这就是我想要的样子(例子)

enter image description here

1 个答案:

答案 0 :(得分:1)

快速回答;

你使用AbsoluteLayout API级别3中的此类已弃用。 请改用FrameLayoutRelativeLayout或自定义布局。

RelativeLayout为例

ImageView在它里面,你给它的宽度和高度相对

 android:layout_width="404dp"
 android:layout_height="550dp"

所以这个尺寸的大小是它消失边界的原因  +你的根标签AbsoluteLayout中有填充  如果你想手动给出尺寸,请给出像75 dp

这样的小宽度

你有几种方法可以完成任务

但首先不要尝试手动提供尺寸然后它会以不同的屏幕尺寸显示 ,但对于单个屏幕,您可以提供这些值。 否则你需要使用基于比率的值。了解layout_weight和`

  

What does android:layout_weight mean?

`以及如何使用它,一个简单的例子 - &gt; Android Layout - layoutweight and weightsum 或者实际上你可以根据比率给出尺寸

// get screen sises 
     Display display = this.getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int screenY = size.y;
            int screenX = size.x; 

RelativeLayout.LayoutParams yourView= new RelativeLayout.LayoutParams(screenX, 270 * screenY / 1920);
        yourView.topMargin = 0;
        yourView.leftMargin = 125 * screenX / 1080;
        imgPoints.setLayoutParams(yourView);

并了解RelativeLayoutLinearLayout行为