android - 如何使用 RelativeLayout 以编程方式设置 textview 的重力?

标签 android android-relativelayout baseadapter gravity

我正在开发一个安卓应用。我通过膨胀行来填充 ListView,如下所示:

public class MyListAdapter extends BaseAdapter {
    Context con;
    private LayoutInflater layoutinf;
    ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
    ArrayList<String> items_ = new ArrayList<String>();

    public MyListAdapter(ChatActivity context) {
        con = context;
    }

    public int getCount() {
        return item_id.size();
    }

    public Object getItem(int position) {
        return item_id.size();
    }

    public long getItemId(int position) {
        return item_id.get(position).hashCode();
    }

    public View getView(final int position, View arg1, ViewGroup arg2) {

        View v = arg1;
        ViewHolder holder = null;
        if (v == null) {
            layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutinf.inflate(R.layout.row_chat, null);
            holder = new ViewHolder();

        //  holder.tv_contact = (TextView) v.findViewById(R.id.phone_num);
            holder.tv_sms_body = (TextView) v.findViewById(R.id.msg_body);
            holder.tv_time = (TextView) v.findViewById(R.id.time);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        if(item_flag.get(position).equals("1"))
        {
            holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_green);

        }
                    else if(item_flag.get(position).equals("0"))
                    {
                        holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_yellow);

                    }


        //holder.tv_contact.setText("" + item_phone_num.get(position));
        holder.tv_sms_body.setText(item_msg_body.get(position));
        holder.tv_time.setText(item_time.get(position));

        return v;
    }
}

布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">



<TextView
    android:id="@+id/msg_body"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@id/phone_num"
    android:layout_below="@id/phone_num"
    android:text="SMS"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@id/msg_body"
    android:text="time"
    android:textSize="12dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

如何在上面的代码中使用 RelativeLayout 以编程方式正确设置 TextView 重力???

最佳答案

你可以这样设置..

    RelativeLayout.LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    textView.setLayoutParams(params);

关于android - 如何使用 RelativeLayout 以编程方式设置 textview 的重力?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22498427/

相关文章:

android - 找不到带有 altbeacon 库的信标

android - Google Map API v2 充气机在旧手机上失败

android - XML Relative Layout order change Crashes 程序 (Android)

android - 在 BaseAdapter android 中停止媒体播放器

java - 从服务访问适配器的值

android - NullPointerException 自定义 ListView 适配器

java - Android客户端-Java服务器,Socket不会抛出异常

SQLite:防止重复

java - android:layout_alignParentTop ="true"和 android:layout_centerVertical ="true"不设置 imageView 在屏幕顶部居中

android - 填充 CollapsingToolBarLayout 剩余空间的 RelativeLayout 高度