如何只在文本在android中的位置使文本可点击?

时间:2013-08-31 18:35:00

标签: android android-linearlayout

在我的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);

1 个答案:

答案 0 :(得分:1)

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

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);