RelativeLayout的属性alignWithParentIfMissing没有给出预期的结果

时间:2012-04-05 22:40:30

标签: android layout relativelayout

提前感谢您的时间。 我使用RelativeLayout来显示我的列表项。我有图像按钮左边与父对齐和图像右侧的一堆TextViews。 只有始终可见的文本视图为 ordersummaryrow.itemtitle ordersummaryrow.itemprice 。 我根据CustomAdapter中的条件设置其余文本视图的可见性。 例如,如果提供特殊请求,则应显示在底部,如果提供了项目选项,则在请求之前,如果提供了额外项目,则在项目选项和最后主项目之上。 此外,如果其他任何文本视图都不可见,我希望主项目标题集中对齐,如解释here所述。 但这不是我用这种行布局得到的。如果提供特殊请求,那就是我看到的唯一的文本视图。它甚至没有显示主项目标题。 我相信它是可行的,但我必须遗漏一些东西。 感谢您的帮助。

ordersummaryrow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listitem_background"
    >
<LinearLayout
    android:id="@+id/ordersummaryrow.buttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dp">
<ImageView
    android:id="@+id/ordersummaryrow.delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/orderdelete"
    android:layout_weight="1" />
<ImageView
    android:id="@+id/ordersummaryrow.update"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/orderedit"
    android:layout_weight="1" />
</LinearLayout> 
<TextView
    android:id="@+id/ordersummaryrow.splrequest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/custom_black_color"
    android:layout_toRightOf="@id/ordersummaryrow.buttons"
    android:layout_alignParentBottom="true" />
<TextView
    android:id="@+id/ordersummaryrow.itemoption"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/custom_black_color"
    android:text="Item Option"
    android:layout_toRightOf="@id/ordersummaryrow.buttons"
    android:layout_above="@id/ordersummaryrow.splrequest" 
    android:layout_alignWithParentIfMissing="true"
    />
<TextView
    android:id="@+id/ordersummaryrow.extraitem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/custom_black_color"
    android:text="Extra Item Title"
    android:layout_toRightOf="@id/ordersummaryrow.buttons"
    android:layout_above="@id/ordersummaryrow.itemoption" 
    android:layout_alignWithParentIfMissing="true"
    />
<TextView
    android:id="@+id/ordersummaryrow.extraitemprice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/custom_black_color"
    android:text="$"
    android:layout_above="@id/ordersummaryrow.itemoption" 
    android:layout_alignParentRight="true"
    android:layout_alignWithParentIfMissing="true" 
    />

<TextView
    android:id="@+id/ordersummaryrow.itemtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/custom_black_color"
    android:layout_above="@id/ordersummaryrow.extraitem" 
    android:layout_toRightOf="@id/ordersummaryrow.buttons"
    android:layout_alignWithParentIfMissing="true"
    android:gravity="center_vertical"
    android:text="Item Title" />

<TextView
    android:id="@+id/ordersummaryrow.itemprice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/custom_black_color"
    android:text="$$"
    android:layout_alignTop="@id/ordersummaryrow.itemtitle"
    android:layout_alignParentRight="true" 
    />
</RelativeLayout>

这是自定义适配器中的GetView()

@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if(row == null)
        {
            LayoutInflater vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            try{
                row = vi.inflate(R.layout.ordersummaryrow, null);
            }
            catch(Exception e)
            {
                Log.i("SUBMENU", e.getMessage());
            }
        }
        OrderSummaryRowViewHolder holder = (OrderSummaryRowViewHolder)row.getTag();
        if(holder == null)
        {
            holder =  new OrderSummaryRowViewHolder(row);
            row.setTag(holder);
        }
        OrderedItem orderItem = m_orderedItem.get(position);
        if (orderItem != null) 
        {
            if(m_updateListener != null)
                holder.m_updateBtn.setOnClickListener(m_updateListener);
            else
                Log.i("OrderSummaryRowAdapter", "Update Listener is not registered");

            if(m_deleteListener != null)
                holder.m_deleteBtn.setOnClickListener(m_deleteListener);
            else
                Log.i("OrderSummaryRowAdapter", "Delete Listener is not registered");

            holder.m_titleTxt.setText(orderItem.get_orderItem().getName());
            holder.m_priceTxt.setText("$" + (String.valueOf(orderItem.get_orderItem().getPrice())));
            if(orderItem.get_extraItems().size() == 0) //No extra items added so hide corresponding text box
            {
                holder.m_extraItemTxt.setVisibility(View.GONE);
                holder.m_extraItemPriceTxt.setVisibility(View.GONE);
            }
            else
            {
                StringBuilder sbItemName = new StringBuilder();
                StringBuilder sbItemPrice = new StringBuilder();
                for (SubMenu extraItem : orderItem.get_extraItems()) 
                {
                    sbItemName.append(extraItem.getName());
                    sbItemName.append("\n");
                    sbItemPrice.append("$" + (String.valueOf(extraItem.getPrice())));
                    sbItemPrice.append("\n");
                }
                holder.m_extraItemTxt.setText(sbItemName);
                holder.m_extraItemPriceTxt.setText(sbItemPrice);
            }

            if(orderItem.get_options().size() == 0)
                holder.m_optionTxt.setVisibility(View.GONE);
            else
            {
                StringBuilder sbOption = new StringBuilder();
                for (ItemOption option : orderItem.get_options()) 
                {
                    sbOption.append(option.getName());
                    sbOption.append("\n");
                }
                holder.m_optionTxt.setText(sbOption);
            }

            if(orderItem.getspecialRequest() != null && 
                    orderItem.getspecialRequest().length() > 0)
                holder.m_splRequestTxt.setText(orderItem.getspecialRequest());
            else
                holder.m_splRequestTxt.setVisibility(View.GONE);

        }
        return row;
    }

1 个答案:

答案 0 :(得分:0)

在所有if().. else()语句中,添加setVisibility(View.VISIBLE)语句。例如,

if(orderItem.getspecialRequest() != null && 
                orderItem.getspecialRequest().length() > 0){
            holder.m_splRequestTxt.setVisibility(View.VISIBLE);
            holder.m_splRequestTxt.setText(orderItem.getspecialRequest());
        }else{
            holder.m_splRequestTxt.setVisibility(View.GONE);
        }

同样适用于我们所有其他案例。

<强>更新

请尝试此代码并检查其是否有效

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    if(row == null)
    {
        LayoutInflater vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        try{
            row = vi.inflate(R.layout.ordersummaryrow, null);
        }
        catch(Exception e)
        {
            Log.i("SUBMENU", e.getMessage());
        }
    }
    OrderSummaryRowViewHolder holder = (OrderSummaryRowViewHolder)row.getTag();
    if(holder == null)
    {
        holder =  new OrderSummaryRowViewHolder(row);
        row.setTag(holder);
    }
    OrderedItem orderItem = m_orderedItem.get(position);
    if (orderItem != null) 
    {
        if(m_updateListener != null)
            holder.m_updateBtn.setOnClickListener(m_updateListener);
        else
            Log.i("OrderSummaryRowAdapter", "Update Listener is not registered");

        if(m_deleteListener != null)
            holder.m_deleteBtn.setOnClickListener(m_deleteListener);
        else
            Log.i("OrderSummaryRowAdapter", "Delete Listener is not registered");

        if(orderItem.getspecialRequest() != null && 
                orderItem.getspecialRequest().length() > 0)
            holder.m_splRequestTxt.setText(orderItem.getspecialRequest());
        else
            holder.m_splRequestTxt.setVisibility(View.GONE);

        if(orderItem.get_options().size() == 0)
            holder.m_optionTxt.setVisibility(View.GONE);
        else
        {
            StringBuilder sbOption = new StringBuilder();
            for (ItemOption option : orderItem.get_options()) 
            {
                sbOption.append(option.getName());
                sbOption.append("\n");
            }
            holder.m_optionTxt.setText(sbOption);
        }

        if(orderItem.get_extraItems().size() == 0) //No extra items added so hide corresponding text box
        {
            holder.m_extraItemTxt.setVisibility(View.GONE);
            holder.m_extraItemPriceTxt.setVisibility(View.GONE);
        }
        else
        {
            StringBuilder sbItemName = new StringBuilder();
            StringBuilder sbItemPrice = new StringBuilder();
            for (SubMenu extraItem : orderItem.get_extraItems()) 
            {
                sbItemName.append(extraItem.getName());
                sbItemName.append("\n");
                sbItemPrice.append("$" + (String.valueOf(extraItem.getPrice())));
                sbItemPrice.append("\n");
            }
            holder.m_extraItemTxt.setText(sbItemName);
            holder.m_extraItemPriceTxt.setText(sbItemPrice);
        }

        holder.m_titleTxt.setText(orderItem.get_orderItem().getName());
        holder.m_priceTxt.setText("$" + (String.valueOf(orderItem.get_orderItem().getPrice())));

    }
    return row;
}