【发布时间】:2013-05-20 08:56:02
【问题描述】:
我正在以编程方式在 Android 中创建视图。父布局是RelativLlayout,我在其中添加了两个视图。第一个是ListView,第二个是自定义视图。我希望我的第二个自定义视图位于列表顶部,但不知何故它被隐藏在ListView 后面。如何确保我的自定义视图位于顶部。
【问题讨论】:
标签: android android-relativelayout
我正在以编程方式在 Android 中创建视图。父布局是RelativLlayout,我在其中添加了两个视图。第一个是ListView,第二个是自定义视图。我希望我的第二个自定义视图位于列表顶部,但不知何故它被隐藏在ListView 后面。如何确保我的自定义视图位于顶部。
【问题讨论】:
标签: android android-relativelayout
视图按照您将它们添加到其父视图的顺序放置。最后添加的视图将位于顶部。您可能还想尝试
View.bringToFront()http://developer.android.com/reference/android/view/View.html#bringToFront()
【讨论】:
尝试使用:View.bringToFront();
【讨论】:
解决方案 1 - 查看#bringToFront()
如果使用得当,View.bringToFront() 应该可以工作。也许你忘记了这一点(来源:http://developer.android.com/reference/android/view/View.html#bringToFront()):
在 KITKAT 之前,此方法应该调用 requestLayout() 和 invalidate()
解决方案 2 - 重新附加视图
如果View.bringToFront() 不起作用,请尝试删除并再次添加视图。优点是它在 KITKAT 之前的版本上也有相同的代码。
我在我的代码中这样做:
ViewGroup parentView = (ViewGroup) (listView.getParent());
parentView.removeView(addFab);
parentView.addView(addFab);
【讨论】:
使用 FrameLayout,框架布局在顶部显示最后添加的视图..
【讨论】:
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<YourCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
【讨论】:
请尝试将android:elevation 属性添加到您在 XML 中的视图中。它总是将您的自定义视图放在 ListView 上。
【讨论】:
再次:视图按照您添加的顺序放置到布局中。问题是:它是真的隐藏还是不渲染?检查 LogCat 和控制台是否有错误。如果您只将这个自定义视图添加到您的布局中,它是否可以毫无问题地呈现?如果是这样,请确保在添加 ListView 时确实将自定义添加到同一个父视图(组)。如果您没有进一步了解,请提供相应的代码示例。
【讨论】:
为您想要调出的视图提供一个高程。
android:elevation="2dp"
【讨论】:
这对我有用:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="220dp"
android:gravity="bottom"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/details_wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="16dip"
android:scaleType="centerCrop"
android:src="@drawable/temp_nav_image">
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Kapil Rajput"
android:textStyle="bold"
android:textColor="@android:color/black"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textStyle="bold"
android:text="kapil@gnxtsystems.com"/>
</LinearLayout>
</RelativeLayout>
【讨论】:
我使用了相对布局,视图的顺序以某种方式决定了哪个在顶部。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:splitTrack="false"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
在此布局中,文本视图将位于搜索栏的顶部
【讨论】:
将父视图设为相对布局,并在 xml 中添加两个视图。它不适用于线性布局。 然后以编程方式添加 FirstView.bringToFront()。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
android:orientation="vertical"
android:padding="15dp">
<ImageView
android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="-30dp"
android:layout_marginTop="30dp"
android:src="@mipmap/login_logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/imgIcon"
android:layout_gravity="bottom"
android:background="#B3FFFFFF"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<View 1/>
<View 2/>
</LinearLayout>
Java file
imgIcon.brigtToFront();
【讨论】:
您应该以编程方式使用android:layout_below 或android:layout_above。所以你应该这样做:
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.below_id);
viewToLayout.setLayoutParams(params);
我建议你take a look at this answer
希望对你有帮助
【讨论】: