-1

I have a piece of code that is parsing my xml values to my android activity, i need to show the parsed xml values in two textviews in horizantal orientation programatically, the values of each textview should be in vertical orientation . I got the textviews in unstructured way. I need to show the text views in structured way. Can anyone please help me to sort out this problem. eg for unstructured output:

      flag
              false
      id
                0                  

I need an output like

      flag   false

      id     0

I created textviews like this

    private void createTextView(String text, LinearLayout root, int textSize,
        int width, int height, int gravity) {
    TextView textView = new TextView(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,
            height);
    if (gravity == Gravity.RIGHT) {
        params.setMargins(300, 0, 0, 0);
    }
    if (gravity == Gravity.LEFT) {
        params.setMargins(150, 0, 0, 0);

    }
    if (gravity == Gravity.CENTER) {
        params.setMargins(20, 5, 0, 5);

    }
    textView.setLayoutParams(params);
    textView.setText(text);
    textView.setTextSize(textSize);
    textView.setGravity(params.gravity);

    textView.setTextColor(context.getResources().getColor(
            android.R.color.black));
    root.addView(textView);
}


           private void createTextViewsWithHorizontalOrient(String textOne,
        String textTwo, LinearLayout root) {
    LinearLayout horizontalLinearLayout = new LinearLayout(context);
    LinearLayout horizontalNextLinearLayout = new LinearLayout(context);

    horizontalLinearLayout.setOrientation(LinearLayout.VERTICAL);
    horizontalLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    horizontalNextLinearLayout.setOrientation(LinearLayout.VERTICAL);
    horizontalNextLinearLayout
            .setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    horizontalNextLinearLayout.setGravity(Gravity.TOP);
    horizontalLinearLayout.setGravity(Gravity.TOP);

    createTextView(textOne, horizontalLinearLayout, 18,
            android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
            android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
            Gravity.LEFT);

    createTextView(textTwo, horizontalNextLinearLayout, 18,
            android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
            android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
            Gravity.RIGHT);
    root.addView(horizontalLinearLayout);
    root.addView(horizontalNextLinearLayout);

}

I got the solution in the method create textview change layout params

               private void createTextView(String text, LinearLayout root,
                          int textSize,int width, int height, int gravity) {

                        TextView textView = new TextView(context);
              LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,
                         height);
              createTextView(textTwo, horizontalNextLinearLayout, 18,
              android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
              android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
              Gravity.LEFT);
4

1 回答 1

0

首先,您的命名约定不好。您有Horizo​​ntalLinearLayout,其方向设置为垂直。

其次,我假设您的根视图是具有垂直方向的 LinearLayout。所以很明显,你得到了这个输出。你应该做的是只创建一个水平方向的_horizo ​​ntalLinearLayout。然后在里面放两个textView,但是你需要玩第二个TextView的边距。

最后,如果我是你,我会为一行创建单独的布局文件(所以水平 LinearLayout 和两个 textView)。然后您可以在 XML 中而不是在代码中获得所有定位。添加布局以查看:

View onewRow = activity.getLayoutInflater().inflate(R.layout.one_row, null, false);
TextView tv1 = onewRow.findViewById(R.id.textView1);
TextView tv2 = onewRow.findViewById(R.id.textView2);
tv1.setText("Flag");
tv2.setText("Your value");
root.addView(onewRow);
于 2013-07-19T13:21:25.080 回答