简体   繁体   English

Android布局以RelativeLayout为中心,用于自定义ListView

[英]Android layout centering in RelativeLayout for custom ListView

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. 为了挽救我的生命,我不能让3个项目垂直居中。 layout_gravity , gravity , layout_centerVertical , none of it works. layout_gravitygravitylayout_centerVertical ,它都layout_gravity I'm sure my issue is some tiny setting to flip somewhere, but I'm really at wits end on this. 我敢肯定我的问题是在某个地方翻转的一个小小的设置,但我真的在这个问题上结束了。

edit: I know the textview is "fill_vertical", that's some random stuff I was trying. 编辑:我知道textview是“fill_vertical”,这是我尝试的一些随机的东西。

This attribute worked for me centering a Button using RelativeLayout : 这个属性适用于我使用RelativeLayout集中Button

android:layout_centerInParent="true"

See this posting: Can you center a Button in RelativeLayout? 看到这篇文章: 你可以将一个按钮置于RelativeLayout中心吗?

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 : 引用无所不在的罗曼盖伊

the correct usage of inflate() in adapters is: 适配器中inflate()的正确用法是:

inflate(layoutId, parent, false); 膨胀(layoutId,parent,false);

Passing the parent (given to you as a parameter in getView()) allows the UI toolkit to create the appropriate LayoutParams object. 传递父(在getView()中作为参数给出)允许UI工具包创建适当的LayoutParams对象。 Passing false tells the toolkit to NOT call parent.addView(theInflateChild), since ListView will do its own magic later on. 传递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. 我将更新我的书籍,以反映未来几周的新建议。

Using RelativeLayout, I had no luck with gravity nor layout_gravity. 使用RelativeLayout,我在重力和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的子项中的,所以如果需要你可以保留它。

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). 一旦我得知它忽略了layout_gravity属性(当然我现在找不到帖子),我抛弃了RelativeLayout。 Either way, nested LinearLayouts did the trick for me: 无论哪种方式,嵌套的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>

您是否尝试过android:gravity =“center_horizo​​ntal”,如此处所述

I did an alternative solution, that worked for me. 我做了一个替代解决方案,对我有用。 I put, on my textbox element, the property android:layout_height="fill_parent". 我在我的textbox元素上放置了属性android:layout_height =“fill_parent”。 This way, the text element filled all hieght of parent, so the contents were aligned correctly :) 这样,文本元素填充了父级的所有高度,因此内容正确对齐:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM