简体   繁体   English

照片上的半透明文本框

[英]Semi-transparent text box over photo

I'm trying to overlay a semi-transparent text box over a photo for description. 我试图在照片上覆盖一个半透明的文本框以进行描述。 Tapping on the screen should make it slide up/down. 轻击屏幕应使其向上/向下滑动。 It should also contain a rating bar in the bottom. 它还应在底部包含一个等级栏。

Much like in Google Goggles http://blogote.com/wp-content/uploads/2010/10/GoogleGoggles1.jpg 就像在Google Goggles中一样http://blogote.com/wp-content/uploads/2010/10/GoogleGoggles1.jpg

How would you do it? 你会怎么做? This is what I have so far: 这是我到目前为止的内容:

<FrameLayout 
    android:id="@+id/mainlayout" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:foregroundGravity="bottom"
    xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
    android:id="@+id/imgview" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<TextView 

    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/hello"/>
</FrameLayout>

This can be easily implemented with a RelativeLayout and a SlidingDrawer widget. 这可以通过RelativeLayoutSlidingDrawer小部件轻松实现。

  1. The RelativeLayout is required so that you can attach the drawer to the bottom of the layout and have it open over the photo. RelativeLayout是必需的,以便您可以将抽屉连接到布局的底部,并将其在照片上打开。
  2. The SlidingDrawer widget has 2 required properties: android:handle and android:content . SlidingDrawer小部件具有2个必需的属性: android:handleandroid:content For clean layouts, you'll probably want these as separate layout files. 对于干净的布局,您可能希望将它们作为单独的布局文件。 For simplicity in the below example, they're included in the same XML layout. 为简单起见,在下面的示例中,它们包含在相同的XML布局中。
  3. The opacity of the background is simple - just pass the android:background property as a ARGB (Alpha RGB) value. 背景的不透明度很简单-只需将android:background属性作为ARGB(Alpha RGB)值传递即可。 Below uses "#BF000000" which is black at roughly 75% opacity. 下面使用“#BF000000”,它是黑色的,不透明度大约为75%。
  4. Your last step is to hide the handle and add an event handler to your ImageView and your SlidingDrawer . 您的最后一步是隐藏该句柄,并将事件处理程序添加到ImageViewSlidingDrawer

Just for kicks, here's a full working example... 仅此而已,这是一个完整的工作示例...

Your layout XML: 您的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                >
<ImageView android:id="@+id/image" 
           android:src="@drawable/bunnywaffles"
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent"
           android:layout_alignParentTop="true"
           />
<SlidingDrawer android:id="@+id/SlidingDrawer" 
               android:handle="@+id/slideHandleButton" 
               android:content="@+id/contentLayout"
               android:layout_width="fill_parent"
               android:layout_height="150dip"
               android:layout_alignParentBottom="true" >
    <Button android:id="@+id/slideHandleButton"
            android:layout_width="0dip" 
            android:layout_height="0dip"
            android:visibility="gone"
            />
    <LinearLayout android:id="@+id/contentLayout"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"  
                  android:orientation="vertical" 
                  android:gravity="center" 
                  android:background="#BF000000">
        <LinearLayout android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:gravity="center">
            <Button android:id="@+id/Button01" 
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content" 
                    android:text="Button1" 
                    />
            <Button android:id="@+id/Button02" 
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content" 
                    android:text="Button2" 
                    />
        </LinearLayout>
        <RatingBar android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:numStars="5" />
    </LinearLayout>
</SlidingDrawer>
</RelativeLayout>

And your activity Java: 而您的活动Java:

ImageView _image;
SlidingDrawer _drawer;
Boolean _drawerOpen = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    _image = (ImageView) findViewById( R.id.image );
    _image.setOnClickListener(ToggleDrawer);

    _drawer = (SlidingDrawer) findViewById( R.id.SlidingDrawer );
    _drawer.setOnClickListener(ToggleDrawer);
}

private View.OnClickListener ToggleDrawer = new View.OnClickListener() {
    public void onClick(View view) {
        if ( _drawerOpen )
            _drawer.animateClose();
        else
            _drawer.animateOpen();
        _drawerOpen = !_drawerOpen;
    }
};

This link gives an idea about creating a transparent background color. 该链接给出了有关创建透明背景色的想法。 Here it's #BF000000 . 这是#BF000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM