英文:
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视图中显示,但背景颜色不显示:
从Design视图中看,背景颜色没有显示 - 在我的手机上打开时什么都没有显示,按钮被隐藏:
以下是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="@color/white"
- so each numerical number button is:
android:background="@color/white"
android:text="7"
android:textColor="@color/black"
in colors.xml defined:
<color name="black">#2E2C2C</color>
<color name="white">#FFFFFF</color>
the textColor is displaying in Design view but not the background:
From the Design view the background color is not displaying - when opening up in my phone nothing displays, buttons are hidden:
below is the xml code:
<?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" >
<Button
android:id="@+id/btnAC"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="0"
android:layout_column="0"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="AC"
android:textColor="@color/orange"
android:textSize="30sp" />
<Button
android:id="@+id/btnDel"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="0"
android:layout_column="1"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="DEL"
android:textColor="@color/orange"
android:textSize="30sp" />
<Button
android:id="@+id/btnDivide"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="0"
android:layout_column="2"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="/"
android:textColor="@color/orange"
android:textSize="30sp" />
<Button
android:id="@+id/btnMulti"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="0"
android:layout_column="3"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="*"
android:textColor="@color/orange"
android:textSize="30sp" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="7"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="8"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="9"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="-"
android:textColor="@color/orange"
android:textSize="30sp" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="2"
android:layout_column="0"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="4"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="2"
android:layout_column="1"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="5"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="2"
android:layout_column="2"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="6"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="2"
android:layout_column="3"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="+"
android:textColor="@color/orange"
android:textSize="30sp" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="3"
android:layout_column="0"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="1"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="3"
android:layout_column="1"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="2"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="3"
android:layout_column="2"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="3"
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btnEquals"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="3"
android:layout_rowSpan="2"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:background="@color/orange"
android:backgroundTint="@color/orange"
android:text="="
android:textColor="@color/white"
android:textSize="30sp" />
<Button
android:id="@+id/btnDot"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="4"
android:layout_column="0"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="."
android:textColor="@color/black"
android:textSize="30sp" />
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_row="4"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:background="@color/white"
android:text="0"
android:textColor="@color/black"
android:textSize="30sp" />
</GridLayout>
</LinearLayout>
Here is themes.xml
:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.CalculatorEx" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
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不允许更改按钮背景颜色,因为按钮背景颜色默认为<item name="colorPrimary">@color/purple_500</item>
(来自themes.xml
页面)
修复方法:
- 按钮宽度:只需将每个按钮的
android:layout_width="wrap_content"
更改为类似android:layout_width="50dp"
的内容,现在所有按钮都会显示在手机屏幕上 - 按钮背景颜色:问题出在
themes.xml
页面,具体是<style...>
标签中的parent
属性 - 将其更改为:
<style name="Theme.CalculatorEx" parent="Theme.AppCompat.Light.NoActionBar">
在进行这些更改之后,在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 <item name="colorPrimary">@color/purple_500</item>
(from themes.xml
page)
Fixes:
- Button Width: simply change the
android:layout_width="wrap_content"
for each button - to something like thisandroid:layout_width="50dp"
and now all buttons are displayed on the phone screen - Button bg color: the problem is the
themes.xml
page, specifically theparent
attribute from the<stlye...>
tag - changed this:
<style name="Theme.CalculatorEx" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
to this:
<style name="Theme.CalculatorEx" parent="Theme.AppCompat.Light.NoActionBar">
after those changes, changing bg colors within the xml code (e.g. - activity_main.xml
) works perfectly!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论