Android Studio的GridLayout被隐藏。

huangapple go评论102阅读模式
英文:

Android Studio GridLayout is hidden

问题

我只完成了这个项目的xml部分,因此在.java页面上没有额外的代码。我使用一个LinearLayout作为基础,然后添加一个带有按钮的GridLayout。我可以在Design视图中看到包含在GridLayout中的按钮,但是背景颜色没有显示android:background="@color/white" - 因此,每个数字按钮都是:

android:background="@color/white"
android:text="7"
android:textColor="@color/black"

在colors.xml中定义:

<color name="black">#2E2C2C</color>
<color name="white">#FFFFFF</color>

文本颜色在Design视图中显示,但背景颜色不显示:

Android Studio的GridLayout被隐藏。

从Design视图中看,背景颜色没有显示 - 在我的手机上打开时什么都没有显示,按钮被隐藏:

Android Studio的GridLayout被隐藏。

以下是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textViewHistory"
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:background="@color/white"
            android:gravity="center|end"
            android:textColor="@color/black"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/textViewResult"
            android:layout_width="match_parent"
            android:layout_height="110dp"
            android:background="@color/white"
            android:gravity="center|end"
            android:text="0"
            android:textColor="@color/black"
            android:textSize="60sp"
            tools:ignore="TextSizeCheck" />

    </LinearLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:rowCount="5">

        <!-- 按钮部分未变 -->

    </GridLayout>

</LinearLayout>

这是themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- 基础应用程序主题。 -->
    <style name="Theme.CalculatorEx" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- 主品牌颜色。 -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- 次要品牌颜色。 -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- 状态栏颜色。 -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- 在此自定义主题。 -->
    </style>
</resources>

感谢您的提问!

英文:

I have only completed the xml portion of this project, so there is no added code to the .java page. I am using a LinearLayout as base and then adding a GridLayout with buttons. I can see the buttons contained in the GridLayout in Design view however the background color is not displaying android:background=&quot;@color/white&quot; - so each numerical number button is:

            android:background=&quot;@color/white&quot;
            android:text=&quot;7&quot;
            android:textColor=&quot;@color/black&quot;

in colors.xml defined:

    &lt;color name=&quot;black&quot;&gt;#2E2C2C&lt;/color&gt;
    &lt;color name=&quot;white&quot;&gt;#FFFFFF&lt;/color&gt;

the textColor is displaying in Design view but not the background:

Android Studio的GridLayout被隐藏。

From the Design view the background color is not displaying - when opening up in my phone nothing displays, buttons are hidden:

Android Studio的GridLayout被隐藏。

below is the xml code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
&gt;
&lt;TextView
android:id=&quot;@+id/textViewHistory&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;90dp&quot;
android:background=&quot;@color/white&quot;
android:gravity=&quot;center|end&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textViewResult&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;110dp&quot;
android:background=&quot;@color/white&quot;
android:gravity=&quot;center|end&quot;
android:text=&quot;0&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;60sp&quot;
tools:ignore=&quot;TextSizeCheck&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;GridLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:columnCount=&quot;4&quot;
android:rowCount=&quot;5&quot; &gt;
&lt;Button
android:id=&quot;@+id/btnAC&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;0&quot;
android:layout_column=&quot;0&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;AC&quot;
android:textColor=&quot;@color/orange&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnDel&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;0&quot;
android:layout_column=&quot;1&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;DEL&quot;
android:textColor=&quot;@color/orange&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnDivide&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;0&quot;
android:layout_column=&quot;2&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;/&quot;
android:textColor=&quot;@color/orange&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnMulti&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;0&quot;
android:layout_column=&quot;3&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;*&quot;
android:textColor=&quot;@color/orange&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn7&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;1&quot;
android:layout_column=&quot;0&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;7&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn8&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;1&quot;
android:layout_column=&quot;1&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;8&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn9&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;1&quot;
android:layout_column=&quot;2&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;9&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnMinus&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;1&quot;
android:layout_column=&quot;3&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;-&quot;
android:textColor=&quot;@color/orange&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn4&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;2&quot;
android:layout_column=&quot;0&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;4&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn5&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;2&quot;
android:layout_column=&quot;1&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;5&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn6&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;2&quot;
android:layout_column=&quot;2&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;6&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnPlus&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;2&quot;
android:layout_column=&quot;3&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;+&quot;
android:textColor=&quot;@color/orange&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;3&quot;
android:layout_column=&quot;0&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;1&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn2&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;3&quot;
android:layout_column=&quot;1&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;2&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn3&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;3&quot;
android:layout_column=&quot;2&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;3&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnEquals&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;3&quot;
android:layout_rowSpan=&quot;2&quot;
android:layout_rowWeight=&quot;1&quot;
android:layout_column=&quot;3&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/orange&quot;
android:backgroundTint=&quot;@color/orange&quot;
android:text=&quot;=&quot;
android:textColor=&quot;@color/white&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnDot&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;4&quot;
android:layout_column=&quot;0&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;.&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn0&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;80dp&quot;
android:layout_row=&quot;4&quot;
android:layout_column=&quot;1&quot;
android:layout_columnSpan=&quot;2&quot;
android:layout_columnWeight=&quot;1&quot;
android:background=&quot;@color/white&quot;
android:text=&quot;0&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;/GridLayout&gt;
&lt;/LinearLayout&gt;

Here is themes.xml:

&lt;resources xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
&lt;!-- Base application theme. --&gt;
&lt;style name=&quot;Theme.CalculatorEx&quot; parent=&quot;Theme.MaterialComponents.DayNight.DarkActionBar&quot;&gt;
&lt;!-- Primary brand color. --&gt;
&lt;item name=&quot;colorPrimary&quot;&gt;@color/purple_500&lt;/item&gt;
&lt;item name=&quot;colorPrimaryVariant&quot;&gt;@color/purple_700&lt;/item&gt;
&lt;item name=&quot;colorOnPrimary&quot;&gt;@color/white&lt;/item&gt;
&lt;!-- Secondary brand color. --&gt;
&lt;item name=&quot;colorSecondary&quot;&gt;@color/teal_200&lt;/item&gt;
&lt;item name=&quot;colorSecondaryVariant&quot;&gt;@color/teal_700&lt;/item&gt;
&lt;item name=&quot;colorOnSecondary&quot;&gt;@color/black&lt;/item&gt;
&lt;!-- Status bar color. --&gt;
&lt;item name=&quot;android:statusBarColor&quot;&gt;?attr/colorPrimaryVariant&lt;/item&gt;
&lt;!-- Customize your theme here. --&gt;
&lt;/style&gt;
&lt;/resources&gt;

I don't think I need to add the .java code as I've not touched it - only starting with the xml and then will build the .java - I just want to start from a solid base before proceeding.

thanks in advance!

答案1

得分: 0

有两个问题 - 首先是按钮的宽度对于手机来说太宽了,如果你将手机转到横向模式,按钮将会显示。第二个问题是较新版本的Android Studio不允许更改按钮背景颜色,因为按钮背景颜色默认为&lt;item name=&quot;colorPrimary&quot;&gt;@color/purple_500&lt;/item&gt;(来自themes.xml页面)

修复方法:

  1. 按钮宽度:只需将每个按钮的android:layout_width=&quot;wrap_content&quot;更改为类似android:layout_width=&quot;50dp&quot;的内容,现在所有按钮都会显示在手机屏幕上
  2. 按钮背景颜色:问题出在themes.xml页面,具体是&lt;style...&gt;标签中的parent属性 - 将其更改为:
&lt;style name=&quot;Theme.CalculatorEx&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;

在进行这些更改之后,在xml代码中(例如 - activity_main.xml),更改背景颜色的操作将完美运行!

英文:

There are two problems - first is that the button width is too wide for the phone, if you turn the phone to landscape the buttons will appear. Second issue is that newer versions of Android Studio do not allow you to change button background colors as the button bg color defaults to &lt;item name=&quot;colorPrimary&quot;&gt;@color/purple_500&lt;/item&gt; (from themes.xml page)

Fixes:

  1. Button Width: simply change the android:layout_width=&quot;wrap_content&quot; for each button - to something like this android:layout_width=&quot;50dp&quot; and now all buttons are displayed on the phone screen
  2. Button bg color: the problem is the themes.xml page, specifically the parent attribute from the &lt;stlye...&gt; tag - changed this:
&lt;style name=&quot;Theme.CalculatorEx&quot; parent=&quot;Theme.MaterialComponents.DayNight.DarkActionBar&quot;&gt;

to this:

&lt;style name=&quot;Theme.CalculatorEx&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;

after those changes, changing bg colors within the xml code (e.g. - activity_main.xml) works perfectly!

huangapple
  • 本文由 发表于 2023年3月21日 02:58:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定