英文:
No color is displayed on the button and no rounding is added to the LinearLayout
问题
-
由于某种原因,按钮显示为紫色,而不是我指定的浅灰色。所有路径都是正确的。
在你的colors.xml文件中,确保你的
backgroundButton
颜色定义正确。在你提供的XML中,backgroundButton
被定义为#FFF1F2F4
,这是浅灰色。如果按钮仍然显示为紫色,请确保没有其他地方重新定义了backgroundButton
颜色。 -
关于LinearLayout不应用圆角的问题:
请确保你的
corners_10.xml
文件定义了正确的圆角。你提到XML文件与按钮相同,只是圆角为10dip。确保corners_10.xml
文件中的圆角定义正确,并且在LinearLayout的android:background
属性中正确引用了这个XML文件。如果圆角仍然没有应用,请检查是否有其他属性或样式覆盖了LinearLayout的背景设置。
英文:
I am developing an android application. I want to add some formatting to my button.
My XML files:
background_button.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/backgroundButton" />
<corners android:radius="0dip" />
</shape>
</item>
</selector>
Button XML code:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go_to_course"
android:textAlignment="textStart"
android:drawableRight="@drawable/right_arrow"
android:textColor="@color/black"
android:background="@drawable/background_button" />
- For some reason, a purple color is applied, and not the one I indicated (light gray). All paths are correct.
colors.xml
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="mainBackground">#F5F6F8</color>
<color name="backgroundButton">#FFF1F2F4</color>
</resources>
right_arrow.xml
<vector android:height="25dp" android:viewportHeight="800"
android:viewportWidth="1653" android:width="51.65625dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#00000000"
android:pathData="M29,400H1446.4M1446.4,400L1239.8,500M1446.4,400L1239.8,300"
android:strokeColor="#000000" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="50"/>
</vector>
- At the same time I want to know why they don’t want to apply rounding on the LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/corners_10">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go_to_course"
android:textAlignment="textStart"
android:drawableRight="@drawable/right_arrow"
android:textColor="@color/black"
android:background="@drawable/background_button" />
<TextView
android:id="@+id/textView2"
android:background="@color/backgroundButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/text" />
</LinearLayout>
And the XML file is the same as on the button, just the corners are indicated by 10 dip.
Please help me with two questions. Thank you!
答案1
得分: 1
对于你的第一个问题,你想要改变按钮的背景颜色。要做到这一点,你需要将 Button
替换为 androidx.appcompat.widget.AppCompatButton
。修改后的代码如下:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go_to_course"
android:textAlignment="textStart"
android:drawableRight="@drawable/right_arrow"
android:textColor="@color/black"
android:background="@drawable/background_button" />
至于你的第二个问题,如果你想要 LinearLayout
的圆角可见,你可以添加 android:paddingBottom
。修改后的代码如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/corners_10"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go_to_course"
android:textAlignment="textStart"
android:drawableRight="@drawable/right_arrow"
android:textColor="@color/black"
android:background="@drawable/background_button" />
<TextView
android:id="@+id/textView2"
android:background="@color/backgroundButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/text" />
</LinearLayout>
你可以根据自己的喜好调整 android:paddingBottom
和 android:paddingTop
,这样就能够添加方形元素而不会隐藏圆角。
英文:
For your 1st question, you want to change the background colour of your button. To do that, you have to replace Button
with androidx.appcompat.widget.AppCompatButton
. Modified code:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go_to_course"
android:textAlignment="textStart"
android:drawableRight="@drawable/right_arrow"
android:textColor="@color/black"
android:background="@drawable/background_button" />
As for your 2nd question, if you want the rounded corners of the LinearLayout
to be visible, you can add android:paddingBottom
. Modified code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/corners_10"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/go_to_course"
android:textAlignment="textStart"
android:drawableRight="@drawable/right_arrow"
android:textColor="@color/black"
android:background="@drawable/background_button" />
<TextView
android:id="@+id/textView2"
android:background="@color/backgroundButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/text" />
</LinearLayout>
You can adjust the android:paddingBottom
and android:paddingTop
to your liking. Then you will be able to add square elements without the rounded corners getting hidden.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论