如何制作半透明布局?

时间:2015-11-02 09:36:02

标签: android

我正在尝试制作半透明布局但不起作用,有人可以帮我这个吗?

这是我的清单:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vratsasoftware.tito.texteditor">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Text" />
        <activity
            android:name=".InfoActivity"
            android:theme="@android:style/Theme.Translucent"/>
    </application>

</manifest>

这是我的活动布局:

<?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:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.vratsasoftware.tito.texteditor.InfoActivity"
    android:background="#50ffffff">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginTop="85dp">

        <TextView
            android:id="@+id/textView_title"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="center"
            android:background="@android:color/darker_gray"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/definition"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/textView_title"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignRight="@+id/textView_title"
            android:layout_below="@+id/textView_title"
            android:layout_marginTop="2dp"
            android:background="@android:color/darker_gray"
            android:text="Definition:"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white" />

        <TextView
            android:id="@+id/textView_definition"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_alignEnd="@+id/definition"
            android:layout_alignLeft="@+id/definition"
            android:layout_alignRight="@+id/definition"
            android:layout_alignStart="@+id/definition"
            android:layout_below="@+id/definition"
            android:layout_marginBottom="2dp"
            android:allowUndo="true"
            android:background="@android:color/darker_gray"
            android:text="New Text"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/button_change"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/textView_definition"
            android:layout_alignRight="@+id/textView_definition"
            android:layout_below="@+id/textView_definition"
            android:layout_gravity="right"
            android:layout_marginLeft="2dp"
            android:allowUndo="true"
            android:background="@android:color/darker_gray"
            android:foreground="@drawable/ic_action" />

        <EditText
            android:id="@+id/editText_replace"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button_change"
            android:layout_alignLeft="@+id/textView_definition"
            android:layout_alignStart="@+id/textView_definition"
            android:layout_below="@+id/textView_definition"
            android:layout_toLeftOf="@+id/button_change"
            android:layout_toStartOf="@+id/button_change"
            android:background="@android:color/darker_gray"
            android:hint="Enter word for replace"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />

        <Button
            android:id="@+id/button_cancel"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/button_change"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignRight="@+id/button_change"
            android:layout_below="@+id/button_change"
            android:layout_marginTop="2dp"
            android:background="@android:color/darker_gray"
            android:foreground="@android:drawable/ic_delete"
            android:foregroundGravity="center_horizontal" />


    </RelativeLayout>


</RelativeLayout>

启动活动后,它停止,这是错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vratsasoftware.tito.texteditor/com.vratsasoftware.tito.texteditor.InfoActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

2 个答案:

答案 0 :(得分:2)

更改您的清单文件:

更改以下行

<activity
        android:name=".InfoActivity"
        android:theme="@android:style/Theme.Translucent"/>

TO:

<activity
        android:name=".InfoActivity"
        android:theme="@style/Base.TextAppearance.AppCompat"/>

答案 1 :(得分:-1)

我希望这会对你有所帮助 在java代码中获取所需布局的id 然后将布局的不透明度设置为较小的数字