【问题标题】:Transparency on image and text图像和文本的透明度
【发布时间】:2017-12-17 09:57:24
【问题描述】:

我想在 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.testforme.android.sample.ItemListActivity">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:background="#FFF" >

    <ImageView
        android:id="@+id/article_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:src="@drawable/item_img"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#7000"
        android:orientation="vertical"
        android:paddingBottom="15dp"
        android:paddingTop="15dp" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:shadowColor="#000"
            android:shadowDx="3"
            android:shadowDy="3"
            android:shadowRadius="6"
            android:text="Styling Android"
            android:textColor="#FFF"
            android:textSize="36sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="A guide to applying styles and themes to Android apps"
            android:textColor="#CCC"
            android:textSize="12sp"/>
    </LinearLayout>
</RelativeLayout>



</LinearLayout>

但我的结果是:

如何将图片和文字设置为示例图片?

【问题讨论】:

  • 使用FrameLayout

标签: android textview imageview styles


【解决方案1】:

与示例相同 :) 和 FLATTENED

 <?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"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 android:id="@+id/frameLayout"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent">

<ImageView
    android:id="@+id/article_img"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/item_img"/>


        <TextView
            android:layout_above="@+id/lower_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_grey"
            android:gravity="center"
            android:shadowColor="#000"
            android:shadowDx="3"
            android:shadowDy="3"
            android:shadowRadius="6"
            android:text="Styling Android"
            android:textColor="#FFF"
            android:textSize="36sp"
            android:textStyle="bold"/>

        <TextView
            android:layout_marginBottom="4dp"
            android:id="@+id/lower_text"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_grey"
            android:gravity="center"
            android:text="A guide to applying styles and themes to Android apps"
            android:textColor="#CCC"
            android:textSize="12sp"/>
    </RelativeLayout>

以及可绘制的 bg_grey.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#a2515350"/>
        </shape>
    </item>
</selector>

【讨论】:

  • 我确定LinearLayout可以省略。
  • @NoiseGenerator 但您仍然需要一个父级来包装两个文本视图以使透明视图大小包含它们对吗?
  • 没有。透明度在 TextView 本身上。相对于它悬停的 ImageView 而言,它是透明的。它们以这样一种方式定位,它们相互重叠。
  • @NoiseGenerator 现在好点了吗 :)?
  • Muuuuuuuuuuuu 确实更好!迷人的!! +1
【解决方案2】:

试试这个:

  <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/article_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:src="@drawable/item_img" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <view
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.4"
            android:background="#7000" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="15dp"
            android:paddingTop="15dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:shadowColor="#000"
                android:shadowDx="3"
                android:shadowDy="3"
                android:shadowRadius="6"
                android:text="Styling Android"
                android:textColor="#FFF"
                android:textSize="36sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="A guide to applying styles and themes to Android apps"
                android:textColor="#CCC"
                android:textSize="12sp" />
        </LinearLayout>
    </FrameLayout>
</RelativeLayout>

【讨论】:

  • 嵌套布局太多。这对表演非常不利。展平你的设计!
猜你喜欢
  • 2014-07-21
  • 2019-03-28
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 2013-09-03
  • 2014-05-31
相关资源
最近更新 更多