[英]How to center Textview inside Listview using RelativeLayout?
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.activity_haltestellen, R.id.tvHaltestellen, HaltestellenListe);
lvH.setAdapter(arrayAdapter);
this is how the stuff is set and .xml: 这是设置和.xml的方式:
<?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/lvHaltestellen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<TextView
android:id="@+id/tvHaltestellen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
I get the StringArray from a database. 我从数据库中获取StringArray。 so how can center the tv inside the listview?
那么如何将电视置于listview内部? realy bad thing
真的不好
I'm not quite sure what do you want to do, but if you want to center the TextView
inside the RelativeLayout
then remove 我不太确定要做什么,但是如果要在
RelativeLayout
居中放置TextView
,请删除
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
from TV and try adding something like android:layout_centarInParent="true"
, and in the relative layout change match_parent
to wrap_contents
从电视上尝试添加
android:layout_centarInParent="true"
,然后在相对布局中将match_parent
更改为wrap_contents
Try to add this one: android:layout_centerInParent="true"
尝试添加此代码:
android:layout_centerInParent="true"
<TextView
android:id="@+id/tvHaltestellen"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
You could anchor the TextView
to the top and bottom of the ListView
(and optionally left and right too, depending on the desired effect). 您可以将
TextView
锚定到ListView
的顶部和底部(也可以根据需要的效果左右固定)。 That will make the TextView
equally tall (and optionally wide), so use a center gravity to position the text in the middle. 这将使
TextView
同样高(并可以选择变宽),因此请使用中心重力将文本放置在中间。
<?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/lvHaltestellen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/tvHaltestellen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/lvHaltestellen"
android:layout_alignBottom="@+id/lvHaltestellen"
android:layout_alignLeft="@+id/lvHaltestellen"
android:layout_alignRight="@+id/lvHaltestellen"
android:gravity="center" />
</RelativeLayout>
If you're planning to add a background colour to the TextView
, you'll need to wrap it inside another transparent container (eg a FrameLayout
), to avoid the colour from obscuring what's displayed in the list. 如果您打算将背景色添加到
TextView
,则需要将其包装在另一个透明容器(例如FrameLayout
)中,以避免该颜色遮盖列表中显示的内容。 Something like: 就像是:
<?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/lvHaltestellen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/lvHaltestellen"
android:layout_alignLeft="@+id/lvHaltestellen"
android:layout_alignRight="@+id/lvHaltestellen"
android:layout_alignTop="@+id/lvHaltestellen" >
<TextView
android:id="@+id/tvHaltestellen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/black" />
</FrameLayout>
</RelativeLayout>
Alternatively, if you decide to make the ListView
fill up the whole height of the RelativeLayout
, you don't need to anchor the TextView
to the list anymore. 另外,如果您决定使
ListView
填满RelativeLayout
的整个高度,则不再需要将TextView
锚定到列表。 As already pointed out by @user1796624, you can then just center the TextView
. 正如@ user1796624所指出的,您可以然后居中放置
TextView
。
As per your earlier comment on someone else's answer: 根据您之前对他人答案的评论:
the textview is shown inside the listview.
文本视图显示在列表视图中。
I understand what you're trying to say here, but please do realize that the TextView
does not sit inside the ListView
, but rather floats on top of it. 我知道您在这里说的是什么,但请务必意识到
TextView
不在 ListView
,而是浮动在它之上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.