如何在表行的单个列中添加两个文本视图

时间:2016-06-20 09:22:07

标签: android textview

如何在表格行的一列中添加另一个文本视图? 我试过的是

LinearLayout parentLastName = new LinearLayout(getActivity());


parentLastName.setLayoutParams(new TableRow.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

parentLastName.setBackgroundResource(R.drawable.picked_ticket_border);

RelativeLayout layout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);


RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);    
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

/** Creating a TextView to add to the row **/
TextView mLastNameTV = new TextView(getActivity());
mLastNameTV.setText(routeOverviewDataList.get(i).getDeliveryLastName());
// mLastNameTV.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
mLastNameTV.setId(i);
mLastNameTV.setTextColor(Color.BLACK);
mLastNameTV.setTypeface(DeliveredApp.sFontOpenSansRegular);
//mLastNameTV.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

TextView test = new TextView(getActivity());
test.setText("sa");
test.setTextColor(Color.BLACK);
params2.addRule(RelativeLayout.BELOW, mLastNameTV.getId());

layout.addView(mLastNameTV, params1);
layout.addView(test, params2);
parentLastName.addView(layout);
mTableRow.addView(parentLastName);

但是只给了一个力量。我可以添加任何一个文本视图,它将顺利显示。是否可以在表格行的单个列中添加两个文本视图?

enter image description here

日志

  06-20 06:05:54.905 20539-20561/ A/libc: Fatal signal      11 (SIGSEGV), code 1, fault addr 0x6e656c46 in tid 20561 (RenderThread)
  06-20 06:05:55.068 1936-1936/? E/EGL_emulation: tid 1936:        eglCreateSyncKHR(1209): error 0x3004 (EGL_BAD_ATTRIBUTE)
  06-20 06:05:55.070 1901-1966/system_process E/InputDispatcher: channel '   1112d0c1 .ui.MainActivity (server)' ~    Channel is unrecoverably broken and will be disposed!
 06-20 06:05:55.074 1901-1980/system_process E/ConnectivityService:     RemoteException caught trying to send a callback msg for NetworkRequest [ id=37,     legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN] ]
  06-20 06:05:55.188 147-147/? E/lowmemorykiller: Error opening   /proc/20539/oom_score_adj; errno=2

3 个答案:

答案 0 :(得分:2)

试试这可能会对你有所帮助

    LinearLayout parentLastName = new LinearLayout(getActivity());
    parentLastName.setOrientation(LinearLayout.VERTICAL);

    parentLastName.setBackgroundResource(R.drawable.picked_ticket_border);

    TableRow mTableRow= new TableRow(getActivity());
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    mTableRow.setLayoutParams(lp);


    //** Creating a TextView to add to the row **//*
    TextView mLastNameTV = new TextView(getActivity());
    mLastNameTV.setText(routeOverviewDataList.get(i).getDeliveryLastName());
    // mLastNameTV.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
    mLastNameTV.setTextColor(Color.BLACK);
    mLastNameTV.setTypeface(DeliveredApp.sFontOpenSansRegular);

    //mLastNameTV.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    TextView test = new TextView(getActivity());
    test.setText("sa");
    test.setTextColor(Color.BLACK);


    parentLastName.addView(mLastNameTV);
    parentLastName.addView(test);
    mTableRow.addView(parentLastName);
    tbl.addView(mTableRow);

答案 1 :(得分:1)

您不必添加两个文本视图,使用一个文本视图并将文本连接到" \ n"会这样做:

yourTextView.setText("sa\nNext Line");

答案 2 :(得分:0)

在单个textview中使用spannable字符串在第二个文本中使用可点击范围(此处当您点击'字符串'时):

SpannableString ss = new SpannableString("Example string");
ss.setSpan(new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        // Do what you need to do here when clicked    
    }
}, 8, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.settext(ss);
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());