1

在我的 android 应用程序中,我创建了一个水平的线性布局,并放置了两个文本视图。第一个文本视图的布局权重应该为 1,因此它试图最大化其宽度。第一个文本视图也是可点击的。我怎样才能让它只有文本部分是可点击的?目前,文本视图的全宽是可点击的......

谢谢

    final LinearLayout UserLayout = new LinearLayout(this);
    UserLayout.setOrientation(LinearLayout.VERTICAL);
    UserLayout.setPadding(20, 20, 20, 20);
    LinearLayout.LayoutParams taskMargin = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    UserLayout.setLayoutParams(taskMargin);
    personobj.layout = UserLayout;

    TextView name = new TextView(this);
    name.setText(Html.fromHtml("<u>" + personobj.lastname + ", " + personobj.firstname + "</u>"));
    name.setTextAppearance(this, R.style.text_link);
    name.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 1));
    name.setClickable(true);
    name.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (MyHTTPFunctions.isNetworkAvailable(context)) {
                MyInterface.RegisterForClose(ActivityEditUsers.this);

                AsyncGetProfileInfo GetProfileTask = new AsyncGetProfileInfo(ActivityEditUsers.this, personobj.ID);
                GetProfileTask.execute();
            } else {
                Toast.makeText(context, context.getResources().getString(R.string.no_internet), Toast.LENGTH_LONG).show();
            }
        }
    });

    final TextView num = new TextView(this);
    num.setText(String.format("%d", index+1));
    num.setPadding(10, 10, 10, 10);
    num.setGravity(Gravity.CENTER);
    num.setTextAppearance(this, R.style.title_numbering);
    num.setBackgroundResource(R.drawable.bg_title_numbering);

    LinearLayout OptionLL = new LinearLayout(this);
    OptionLL.setOrientation(LinearLayout.HORIZONTAL);

    OptionLL.addView(name);
    OptionLL.addView(num);

    UserLayout.addView(OptionLL);
4

1 回答 1

1

将第一个 TextView 放在 LinearLayout 中。将此 LinearLayout 的权重设置为1. 使用参数 WRAP_CONTENT 和无权重将 TextView 添加name到此 LinearLayout。并保持 OnClickListener 设置为name

LinearLayout firstTextViewHolder = new LinearLayout(this);

firstTextViewHolder.setLayoutParams(new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 1));

TextView name = new TextView(this);
name.setText(Html.fromHtml("<u>" + personobj.lastname + ", " + personobj.firstname + "</u>"));
name.setTextAppearance(this, R.style.text_link);
name.setLayoutParams(new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

name.setClickable(true);
name.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        if (MyHTTPFunctions.isNetworkAvailable(context)) {
            MyInterface.RegisterForClose(ActivityEditUsers.this);

            AsyncGetProfileInfo GetProfileTask = new AsyncGetProfileInfo(ActivityEditUsers.this, personobj.ID);
            GetProfileTask.execute();
        } else {
            Toast.makeText(context, context.getResources().getString(R.string.no_internet), Toast.LENGTH_LONG).show();
        }
    }
});

firstTextViewHolder.addView(name);

LinearLayout OptionLL = new LinearLayout(this);
OptionLL.setOrientation(LinearLayout.HORIZONTAL);

OptionLL.addView(firstTextViewHolder);
OptionLL.addView(num);

UserLayout.addView(OptionLL);
于 2013-08-31T20:53:35.480 回答