[英]How to set a background image or color or gradient for menu items in android action bar
[英]How to set background image with transparent gradient color in android?
我是android开发的新手。 我的目标是将背景图像设置为渐变透明色,我尝试了许多方法,但我无法实现这一目标。有谁能帮助我? 我已附上示例屏幕截图。
源代码
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bc"
android:orientation="vertical"
android:paddingLeft="20dp"
android:alpha="0.7"
android:id="@+id/about"
android:paddingRight="20dp"
android:paddingTop="60dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/otp_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/hintStyle">
<EditText
android:id="@+id/mobNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_mob"
android:inputType="number"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@color/colorPrimary"
android:text="@string/submit"
android:textColor="@android:color/white"/>
</LinearLayout>
提前谢谢。
以下是十六进制值-
100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00
如果您要设置50%透明的黑色,请执行以下操作-
<color name="transparentBlack">#80000000</color>
更新:
您可以使用以下渐变来设置背景-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#008000"
android:endColor="#FFFF00"
android:angle="90"
android:type="linear"
android:dither="true"
/>
</shape>
有一个颜色名称“ GreenYellow”,如果您要查找的颜色代码如下-
GreenYellow: ADFF2F
只需附加
100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00
这两个字符位于您的绿色前面。 例如, #62cf51是您的起始绿色,那么您可以使用#9962cf51颜色作为透明度。 在此示例中,我添加了60%的透明度,您可以根据需要使用。
试试这个,这对我来说是工作,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/images"
android:text="Hello World!" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/trans"
android:paddingBottom="50dp"
android:paddingTop="50dp">
<EditText
android:id="@+id/ed_text1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="15dp"
android:background="#80ffffff" />
<EditText
android:id="@+id/ed_text2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@+id/ed_text1"
android:layout_margin="15dp"
android:background="#80ffffff" />
</RelativeLayout>
</RelativeLayout>
and trans.xml drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#6066ff66"
android:centerColor="#6066ff66"
android:endColor="#6066ff66"
android:angle="90"
/>
</shape>
您始终可以通过创建新的可绘制xml资源来实现此目的。 创建一个新的可绘制资源
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="@android:color/transparent"
android:endColor="#20403f4c"
android:angle="90"
android:type="linear"
android:dither="true"
/>
</shape>
将其设置为任何元素的背景。 如果要更改透明度,上述答案将有所帮助。
您可以根据需要进行编辑。 将编辑文本和所有内容添加到以下布局
<?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"
android:id="@+id/about"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:background="@mipmap/ic_launcher"
android:orientation="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:background="@mipmap/ic_launcher"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BF228B22">
</RelativeLayout>
</LinearLayout>
试试这个ans:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff99"
android:orientation="vertical"
android:id="@+id/about"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#20403f4c"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<android.support.design.widget.TextInputLayout
android:id="@+id/otp_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/hintStyle">
<EditText
android:id="@+id/mobNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_mob"
android:inputType="number"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:background="@android:color/transparent"
android:text="submit"
android:textColor="@android:color/black"/>
</RelativeLayout>
或根据您的透明度更改此android:background =“#20403f4c”的背景
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.