照片上的半透明文本框

发布于 2024-10-18 00:23:25 字数 870 浏览 1 评论 0原文

我试图在照片上覆盖一个半透明文本框以进行描述。点击屏幕应使其向上/向下滑动。它还应该在底部包含一个评级栏。

与 Google Goggles 非常相似 http://blogote.com/wp-content/ uploads/2010/10/GoogleGoggles1.jpg

你会怎么做?这是我到目前为止所拥有的:

<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>

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2024-10-25 00:23:25

这可以通过 RelativeLayoutSlidingDrawer 小部件轻松实现。

  1. RelativeLayout 是必需的,以便您可以将抽屉附加到布局的底部并将其在照片上方打开。
  2. SlidingDrawer 小部件有 2 个必需属性:android:handleandroid:content。对于干净的布局,您可能需要将它们作为单独的布局文件。为了简单起见,在下面的示例中,它们包含在相同的 XML 布局中。
  3. 背景的不透明度很简单 - 只需将 android:background 属性作为 ARGB (Alpha RGB) 值传递即可。下面使用“#BF000000”,它是黑色的,不透明度约为 75%。
  4. 最后一步是隐藏句柄并向 ImageViewSlidingDrawer 添加事件处理程序。

只是为了好玩,这里有一个完整的工作示例...

您的布局 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>

以及您的活动 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 can be easily implemented with a RelativeLayout and a SlidingDrawer widget.

  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.
  2. The SlidingDrawer widget has 2 required properties: android:handle and android: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.
  3. The opacity of the background is simple - just pass the android:background property as a ARGB (Alpha RGB) value. Below uses "#BF000000" which is black at roughly 75% opacity.
  4. Your last step is to hide the handle and add an event handler to your ImageView and your SlidingDrawer.

Just for kicks, here's a full working example...

Your layout 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:

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;
    }
};
三月梨花 2024-10-25 00:23:25

此链接
给出了创建透明背景颜色的想法。这是#BF000000

This link
gives an idea about creating a transparent background color. Here it's #BF000000.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文