android-当一个或多个可见性设置为GONE时,将TextViews在RelativeLayout中垂直居中

我正在尝试创建一个包含4个项目的列表项目布局.它与示例here非常相似,但有一些关键区别:

> RelativeLayout layout_height可能大于listPreferredItemHeight
>图像是一个复选框
>共有三个TextViews
>顶部和中间的TextView的可见性可以设置为GONE

我想要的是:如果_label不见了,那么_message和_definition应该在布局中平均加权.如果_label和_message不存在,则_definition应该居中.我尝试了几种高度,宽度,重力,layout_gravity的变化形式,甚至尝试使用LinearLayout解决这个问题(既作为整个布局,又只是包含垂直文本).我可以使它起作用的唯一方法是,如果将layout_height设置为listPreferredItemHeight,但是只有两个文本视图适合.

<?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:minHeight="?android:attr/listPreferredItemHeight">
    <CheckBox android:id="@+id/alarm_list_item_able" 
        android:text=""     
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="True"
        android:layout_alignParentLeft="True"
        android:layout_alignParentBottom="True"
        android:layout_centerVertical="True"
        android:focusable="false"
        android:layout_marginRight="6dp"
        android:onClick="onClick"
        />
    <TextView android:id="@+id/alarm_list_item_label" 
        android:text="Label place holder" 
        android:singleLine="True"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/alarm_list_item_able"
        android:layout_alignParentTop="True"
        android:gravity="center_vertical"
        />
    <TextView android:id="@+id/alarm_list_item_message" 
        android:text="Message place holder"
        android:singleLine="True"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:layout_below="@id/alarm_list_item_label"
        android:layout_toRightOf="@id/alarm_list_item_able"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        />          
    <TextView android:id="@+id/alarm_list_item_location" 
        android:singleLine="True"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"     
        android:layout_below="@id/alarm_list_item_message"
        android:layout_toRightOf="@id/alarm_list_item_able"
        android:layout_marginBottom="5dp"
        android:layout_alignWithParentIfMissing="true"
        android:layout_alignParentRight="true"
        android:paddingBottom="5dp"
        android:text="(0.000, 0.000) 0m"
        android:gravity="center_vertical"
        />
</RelativeLayout>

使用来自blessenm的xml的适配器getView代码

@Override
public View getView(int position, View convertView, ViewGroup parent){
    Log.d(Global.TAG,"Position "+position);
    View newView;
    if (convertView != null ) 
        newView = convertView; 
    else { 
        LayoutInflater layout = LayoutInflater.from(MyActivity.this); 
        newView = layout.inflate(R.layout.alarm_list_item, null); 
    }
    String tmp1 = "Place Holder Label"
    TextView label = (TextView)newView.findViewById(R.id.alarm_list_item_label);
    LayoutParams l = label.getLayoutParams();
    if (tmp1.matches("^\\s*$")){
        label.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,0f));
    } else {
        label.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,1f));
    }
    label.setText(tmp1)

    String tmp2 = "Place Holder Message"
    TextView message = (TextView)newView.findViewById(R.id.alarm_list_item_message);
    l = message.getLayoutParams();
    if (tmp2.matches("^\\s*$")){
        message.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,0f));
    } else {
        message.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,1f));
    }
    message.setText(tmp2);

    TextView location = (TextView)newView.findViewById(R.id.alarm_list_item_location);
    location.setText("location");

    return newView;
}
最佳答案
Layout_gravity仅在线性布局内工作.当您希望所有文本视图的高度均匀时,将它们放置在垂直方向的线性布局中,并将所有文本视图的权重设置为1.因此,现在所有这些人的身高都相等.

当您需要隐藏文本视图时,只需将其layout_weight设置为0,现在剩余的文本视图将占用全部可用空间.而且,如果您给textview重力=“ center”,则文本将在垂直和水平方向上居中.

编辑:xml和getView方法

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
    <CheckBox android:id="@+id/alarm_list_item_able" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:focusable="false" 
        android:layout_marginRight="6dp"
        android:onClick="onClick" 
        android:layout_centerVertical="true"/>
    <LinearLayout android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/alarm_list_item_able" 
        android:orientation="vertical"
        android:layout_centerVertical="true">
        <TextView android:id="@+id/alarm_list_item_label" 
            android:text="Label place holder" 
            android:singleLine="True" 
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"/>
        <TextView android:id="@+id/alarm_list_item_message" 
            android:text="Message place holder"
            android:singleLine="True" 
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@id/alarm_list_item_able"
            android:layout_below="@id/alarm_list_item_label"/>
        <TextView android:id="@+id/alarm_list_item_location" 
            android:singleLine="True"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="(0.000, 0.000) 0m" 
            android:layout_toRightOf="@id/alarm_list_item_able"
            android:layout_below="@id/alarm_list_item_message"/> 
    </LinearLayout> 
</RelativeLayout>

getview方法

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if(convertView == null){
        convertView = inflater.inflate(R.layout.list, null);
        holder = new ViewHolder();
        holder.label = (TextView)convertView.findViewById(
                R.id.alarm_list_item_label);
        holder.msg   = (TextView)convertView.findViewById(
                R.id.alarm_list_item_message);
        holder.loc   = (TextView)convertView.findViewById(
                R.id.alarm_list_item_location);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if(holder != null){
        if(data.get(position).mLabel.equals("")){
            LinearLayout.LayoutParams labelLp = new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,0);
            if(!data.get(position).mMsg.equals("")) {                   
                holder.label.setLayoutParams(labelLp);                  
                holder.msg.setText(data.get(position).mMsg);                    
                holder.loc.setText(data.get(position).mLoc);
            } else {
                holder.label.setLayoutParams(labelLp);                  
                holder.msg.setLayoutParams(labelLp);                    
                holder.loc.setText(data.get(position).mLoc);
            }
        } else {
            holder.label.setText(data.get(position).mLabel);
            holder.msg.setText(data.get(position).mMsg);
            holder.loc.setText(data.get(position).mLoc);
        }
    }
    return convertView;
}

转载注明原文:android-当一个或多个可见性设置为GONE时,将TextViews在RelativeLayout中垂直居中 - 代码日志