按钮上没有显示任何颜色,LinearLayout也没有添加任何圆角。

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

No color is displayed on the button and no rounding is added to the LinearLayout

问题

  1. 由于某种原因,按钮显示为紫色,而不是我指定的浅灰色。所有路径都是正确的。

    在你的colors.xml文件中,确保你的backgroundButton颜色定义正确。在你提供的XML中,backgroundButton被定义为#FFF1F2F4,这是浅灰色。如果按钮仍然显示为紫色,请确保没有其他地方重新定义了backgroundButton颜色。

  2. 关于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" />
  1. 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>
  1. 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:paddingBottomandroid: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:

&lt;androidx.appcompat.widget.AppCompatButton
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;@string/go_to_course&quot;
            android:textAlignment=&quot;textStart&quot;
            android:drawableRight=&quot;@drawable/right_arrow&quot;
            android:textColor=&quot;@color/black&quot;
            android:background=&quot;@drawable/background_button&quot; /&gt;

As for your 2nd question, if you want the rounded corners of the LinearLayout to be visible, you can add android:paddingBottom. Modified code:

&lt;LinearLayout
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:orientation=&quot;vertical&quot;
                android:background=&quot;@drawable/corners_10&quot;
                android:paddingBottom=&quot;10dp&quot;
                android:paddingTop=&quot;10dp&quot;&gt;
    
                &lt;Button
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:text=&quot;@string/go_to_course&quot;
                    android:textAlignment=&quot;textStart&quot;
                    android:drawableRight=&quot;@drawable/right_arrow&quot;
                    android:textColor=&quot;@color/black&quot;
                    android:background=&quot;@drawable/background_button&quot; /&gt;
                &lt;TextView
                    android:id=&quot;@+id/textView2&quot;
                    android:background=&quot;@color/backgroundButton&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:padding=&quot;10dp&quot;
                    android:text=&quot;@string/text&quot; /&gt;
           &lt;/LinearLayout&gt;

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.

huangapple
  • 本文由 发表于 2023年6月11日 21:12:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450623.html
匿名

发表评论

匿名网友

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

确定