java - android studio 中的布局问题。如何让相对布局覆盖整个屏幕而不是停留在左上角?

标签 java android xml android-layout

design layout preview

这是使用拖放方法向我的android studio项目添加相对布局的结果图。这似乎发生在我的许多项目中,然后解决了,我不明白为什么。我对 android 开发以及 java 和 xml 都很陌生,所以我有点困惑。

这是 content_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.roger.myowncalculator.MainActivity"


        tools:showIn="@layout/activity_main">


    </RelativeLayout>


    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.roger.myowncalculator.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

我认为这不会影响它,但这也是我的 MainActivity.java 文件。

package com.example.roger.myowncalculator;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button b1 = (Button)  findViewById(R.id.button1);
    Button b2 = (Button) findViewById(R.id.button2);
    Button b3 = (Button) findViewById(R.id.button3);
    Button b4 = (Button) findViewById(R.id.button4);
    Button b5 = (Button) findViewById(R.id.button5);
    Button b6 = (Button) findViewById(R.id.button6);
    Button b7 = (Button) findViewById(R.id.button7);
    Button b8 = (Button) findViewById(R.id.button8);
    Button b9 = (Button) findViewById(R.id.button9);
    Button b0 = (Button) findViewById(R.id.button0);
    Button bC = (Button) findViewById(R.id.buttonC);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

最佳答案

使用

android:layout_width="match_parent"
android:layout_height="match_parent"

而不是

android:layout_width="wrap_content"
android:layout_height="wrap_content"

wrap_content 表示占用您需要的空间,match_parent 表示如其所示。

关于java - android studio 中的布局问题。如何让相对布局覆盖整个屏幕而不是停留在左上角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35999722/

相关文章:

java - 监控 JDBC 连接池

java - 如何在 Struts2 中为 FreeMarker 结果启用枚举指令?

android - 如何升级 meteor 应用程序

java - Hibernate 上关系复合主键的问题

java - 在选项卡式 Activity 中禁用滑动 (Android Studio)

java - 显示照片android代码

android - 根据 list 中是否设置了 Theme.NoTitleBar,选项卡显示不同

来自 xml 的 Android 本地化字符串

java - 使用 xsl 将 XML 传输到 CSV 期间的空行

C# XML - 读取包含的 XML 文档