Java 自定义ListView的Android布局以RelativeLayout居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1499555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android layout centering in RelativeLayout for custom ListView
提问by MattC
I'm really pulling my hair out over this one. Some background. I have a list of items that all have checkboxes next to them. When you deselect a checkbox, a button appears that allows you to delete the item from the list. This seems backwards at first but we only want "selected" items to be eligible for further processing, etc. This is my layout:
我真的把我的头发拉到这个上面。一些背景。我有一个项目列表,所有项目旁边都有复选框。当您取消选择一个复选框时,会出现一个按钮,允许您从列表中删除该项目。起初这似乎是倒退,但我们只希望“选定”项目有资格进行进一步处理等。这是我的布局:
<RelativeLayout android:id="@+id/rlBlahBlah"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBox android:text=""
android:id="@+id/cbDeleteItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
/>
<TextView android:text=""
android:id="@+id/tvItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:paddingLeft="3dip"
android:paddingRight="3dip"
android:paddingTop="13dip"
android:gravity="fill_vertical"
android:layout_toRightOf="@id/cbDeleteItem"
/>
<Button android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:text="Delete"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:focusable="false"
/>
</RelativeLayout>
I cannot get the 3 items to center vertically in my row to save my life. layout_gravity
, gravity
, layout_centerVertical
, none of it works. I'm sure my issue is some tiny setting to flip somewhere, but I'm really at wits end on this.
我无法让 3 个项目在我的行中垂直居中以挽救我的生命。layout_gravity
, gravity
, layout_centerVertical
, 都不起作用。我确定我的问题是一些微小的设置可以在某个地方翻转,但我真的对此无能为力。
edit: I know the textview is "fill_vertical", that's some random stuff I was trying.
编辑:我知道 textview 是“fill_vertical”,这是我尝试的一些随机内容。
采纳答案by CommonsWare
Your problem is probably not due to the layout, but how you are inflating the layout. In fact, it might even be my fault, depending on where you learned your technique...
您的问题可能不是由于布局,而是您如何夸大布局。事实上,这甚至可能是我的错,这取决于你在哪里学到的技术......
To quote the omnipresent Romain Guy:
引用无处不在的Romain Guy 的话:
the correct usage of inflate() in adapters is:
inflate(layoutId, parent, false);
Passing the parent (given to you as a parameter in getView()) allows the UI toolkit to create the appropriate LayoutParams object. Passing false tells the toolkit to NOT call parent.addView(theInflateChild), since ListView will do its own magic later on.
inflate() 在适配器中的正确用法是:
膨胀(layoutId,父,假);
传递父对象(在 getView() 中作为参数提供给您)允许 UI 工具包创建适当的 LayoutParams 对象。传递 false 会告诉工具包不要调用 parent.addView(theInflateChild),因为 ListView 稍后会发挥自己的作用。
If you use inflate(layoutId, null)
, as I have traditionally advised, life is OK unless you try using RelativeLayout
as the base layout of the rows and try to use vertical centering.
如果您使用inflate(layoutId, null)
,正如我传统上所建议的那样,除非您尝试使用RelativeLayout
作为行的基本布局并尝试使用垂直居中,否则生活还可以。
I will be updating my books and such to reflect the new advice in the coming weeks.
在接下来的几周内,我将更新我的书籍等以反映新的建议。
回答by Carnell
回答by MattC
Here is a layout that ended up doing exactly what I wanted. I ditched RelativeLayout once I learned that it ignores layout_gravity
attributes (of course now I can't find the post). Either way, nested LinearLayouts did the trick for me:
这是一个最终完成我想要的布局。一旦我了解到它忽略了layout_gravity
属性,我就放弃了 RelativeLayout (当然现在我找不到帖子了)。无论哪种方式,嵌套的 LinearLayouts 都帮我解决了这个问题:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:id="@+id/llBlahBlahRowMain"
android:padding="6dip">
<CheckBox android:text=""
android:id="@+id/cbDeleteItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<TextView
android:text="blah blah dynamically replaced text"
android:id="@+id/tvItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14dip"
android:paddingLeft="3dip"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:text="Delete"
android:layout_gravity="center_vertical"
android:focusable="false"
/>
</LinearLayout>
回答by Murphy
Using RelativeLayout, I had no luck with gravity nor layout_gravity.
使用RelativeLayout,我对gravity 和layout_gravity 都没有运气。
But android:layout_centerVertical="true"
worked for me positioned in the child of my RelativeLayout, so you can keep it if you need.
但是android:layout_centerVertical="true"
为我工作,定位在我的 RelativeLayout 的孩子中,因此您可以根据需要保留它。
回答by John
This attribute worked for me centering a Button
using RelativeLayout
:
这个属性对我有用,以Button
using为中心RelativeLayout
:
android:layout_centerInParent="true"
See this posting: Can you center a Button in RelativeLayout?
看到这个帖子: 你能在RelativeLayout中将按钮居中吗?
回答by Luiz Vianna
I did an alternative solution, that worked for me. I put, on my textbox element, the property android:layout_height="fill_parent". This way, the text element filled all hieght of parent, so the contents were aligned correctly :)
我做了一个替代解决方案,这对我有用。我在我的文本框元素上放置了属性 android:layout_height="fill_parent"。这样,文本元素填充了父级的所有高度,因此内容正确对齐:)
<TextView android:id="@+id/principal_li_descricao"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_marginLeft="10dip"
android:textColor="#ff000000"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_alignBottom="@+id/principal_li_icone"
android:layout_toRightOf="@+id/principal_li_icone"/>
Thanks
谢谢