在线性布局中重叠文本视图

3
我的问题是我想在LinearLayout中重叠两个TextView。在智能手机上看起来没问题,但在平板电脑上负边距让我很难处理。
这是我想要实现的布局 enter image description here 以下是代码。我该如何修改它以便让一个TextView重叠在另一个TextView的右上方而不使用负边距?
<FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/bottom_bar_back_with_arrow"

            android:layout_weight="1">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center">

                <TextView
                    android:id="@+id/mShowHideScale"
                    style="@style/BottomBarButton"
                    android:layout_weight="1"
                    android:drawableTop="@drawable/show_calibrate_tool"
                    android:text="Calibrate" />

                <TextView
                    android:id="@+id/badgeRadius"
                    android:layout_width="14dip"
                    android:layout_height="14dip"
                    android:textColor="@color/primary_color"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:background="@drawable/shape_notification"
                    android:layout_marginLeft="-18dp"
                    android:layout_marginTop="-27dp"/>
            </LinearLayout>
          </FrameLayout>

在手机和平板电脑上,DP度量是不同的。可能这就是为什么它让你感到困难的原因。 - Geet Choubey
为什么不使用RelativeLayout或FrameLayout? - Oğuzhan Döngül
这就是为什么我问oguzhand的原因,因为我真的无法弄清楚,整个UI都会因RelativeLayout而变得混乱。 - user5076870
使用相对布局会更容易。 - AbhayBohra
你得到答案了吗? - DKV
还没想出来。 - user5076870
2个回答

1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/White">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.UTU.View.UtuTextView
            android:visibility="visible"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="17sp"
            android:text="" />

        <com.UTU.View.UtuButton
            android:id="@+id/btn_crop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent_full"
            android:textColor="@color/black"
            android:clickable="true"
            android:layout_marginTop="30dp"
            android:text="Select Image" />

        <FrameLayout
            android:background="@drawable/utu_round_background"
            android:layout_marginRight="10dp"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="right">

            <ImageView
                android:background="@color/red"
                android:src="@drawable/icon_user_default"
                android:id="@+id/iv_fragment_dashboard_user_image"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="right" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/icon_profile_border" />
        </FrameLayout>

    </FrameLayout>

</LinearLayout>

尝试这个。我使用我的代码来修改一些布局进行测试,对于圆形绘制,您可以尝试修改图像或按钮的背景。

谢谢kggoh!它在手机上可行,但在平板电脑上仍然失败。在这里查看:http://imgur.com/a/djtyR - user5076870
如何安排两个组件的确切位置,我认为必须留给使用者自行决定。我只展示如何使用帧布局将一个组件放在另一个组件之上。 - kggoh
是的,谢谢。我仍然需要设置一个以dp为单位的边距,这个边距会因设备而异。 - user5076870
1
不,你必须充分利用match_parent。例如,如果第一个framelayout的高度和宽度都设置为match_parent,则第二个framelayout也应设置为match_parents。如果你在一个设备上设置了正确的位置,基本上所有设备都应该正常运作,因为match_parents基本上锁定了图像。我没有测试过,所以我只能猜测。 - kggoh
你解决了这个问题吗?我的解决方案有帮助吗? - kggoh

0

这是我在代码中的做法

<RelativeLayout
    android:layout_width="@dimen/menuitem_width"
    android:layout_height="@dimen/toolbar_height">

    <ImageView
        android:id="@+id/menu_layout_notification_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/mi_menu_notification"/>

        <RelativeLayout
            android:id="@+id/menu_layout_notifcation_newIndicatorWrapper"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_marginTop="-5dp"
            android:layout_marginEnd="-5dp"
            android:background="@drawable/background_red_notif"
            android:layout_alignTop="@+id/menu_layout_notification_icon"
            android:layout_alignEnd="@+id/menu_layout_notification_icon"
            android:visibility="gone">

...
        </RelativeLayout>
</RelativeLayout>

通过将其与ImageView的顶部和末端对齐,RelativeLayout的顶部和末端与ImageView的顶部和末端对齐。然后我添加负边距,在ImageView内留下5 dp。

仍然是同样的问题,不幸的是。无论如何,谢谢你,Kevin。 - user5076870

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接