在我的 android 应用程序中,我创建了一个水平线性布局,并放置了两个 TextView 。第一个 TextView 的布局权重应该为 1,因此它试图最大化其宽度。第一个 TextView 也是可以单击的。我怎样才能得到它,以便只有它的文本部分可以点击? 目前, TextView 的全宽是可点击的...
谢谢
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);
最佳答案
将第一个 TextView 放在 LinearLayout 中。将此 LinearLayout 的权重设置为 1
。将 TextView name
添加到此 LinearLayout 中,参数为 WRAP_CONTENT 并且没有权重。并在 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);
关于android - 如何使文本仅在文本位于android中的位置可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18551543/