【问题标题】:Add TextView Dynamically To a view Android将 TextView 动态添加到 Android 视图中
【发布时间】:2015-08-22 05:54:57
【问题描述】:

我想在布局中动态显示多个文本视图。 假设,用户从下拉菜单中选择 10 个项目,然后我可以将其显示为一个接一个的布局。我需要与附加图像相同的视图 -

我在列表中选择了项目,现在我不知道如何以所需的方式显示数据。当我尝试列表视图时,所有数据都垂直显示。 当我使用 Linearlayout 时,数据可以按水平或垂直顺序添加。所以我没有得到正确的数据显示方式。

请帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 当我使用网格视图时,它有空间用于对齐数据。列之间

标签: android


【解决方案1】:

你要找的是芯片。为什么不用材料设计芯片,你可以参考这里。 http://www.google.com/design/spec/components/chips.html#
如果您对使用芯片不感兴趣,您可以像使用网格视图一样实现,只需有一排网格视图并用一个单元格填充它

【讨论】:

    【解决方案2】:

    通常,如果您使用水平方向的 LinearLayout,它会按您的意愿放置您的项目。

    【讨论】:

      【解决方案3】:

      试试这个..我曾经使用自定义视图来实现。如果你没问题就试试吧..

      自定义视图类是。

          public class WrapLayout extends ViewGroup {
      
          private int paddingHorizontal;
          private int paddingVertical;
      
          public WrapLayout(Context context) {
              super(context);
              init();
          }
      
          public WrapLayout(Context context, AttributeSet attrs) {
              this(context, attrs, 0);
          }
      
          public WrapLayout(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
              init();
          }
      
          private void init() {
              paddingHorizontal = getResources().getDimensionPixelSize(10);
              paddingVertical = getResources().getDimensionPixelSize(10);
          }
      
          @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
              int childLeft = getPaddingLeft();
              int childTop = getPaddingTop();
              int lineHeight = 0;
              // 100 is a dummy number, widthMeasureSpec should always be EXACTLY for FlowLayout
              int myWidth = resolveSize(100, widthMeasureSpec);
              int wantedHeight = 0;
              for (int i = 0; i < getChildCount(); i++) {
                  final View child = getChildAt(i);
                  if (child.getVisibility() == View.GONE) {
                      continue;
                  }
                  // let the child measure itself
                  child.measure(
                          getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutParams().width),
                          getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutParams().height));
                  int childWidth = child.getMeasuredWidth();
                  int childHeight = child.getMeasuredHeight();
                  // lineheight is the height of current line, should be the height of the heightest view
                  lineHeight = Math.max(childHeight, lineHeight);
                  if (childWidth + childLeft + getPaddingRight() > myWidth) {
                      // wrap this line
                      childLeft = getPaddingLeft();
                      childTop += paddingVertical + lineHeight;
                      lineHeight = childHeight;
                  }
                  childLeft += childWidth + paddingHorizontal;
              }
              wantedHeight += childTop + lineHeight + getPaddingBottom();
              setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec));
          }
      
          @Override
          protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
              int childLeft = getPaddingLeft();
              int childTop = getPaddingTop();
              int lineHeight = 0;
              int myWidth = right - left;
              for (int i = 0; i < getChildCount(); i++) {
                  final View child = getChildAt(i);
                  if (child.getVisibility() == View.GONE) {
                      continue;
                  }
                  int childWidth = child.getMeasuredWidth();
                  int childHeight = child.getMeasuredHeight();
                  lineHeight = Math.max(childHeight, lineHeight);
                  if (childWidth + childLeft + getPaddingRight() > myWidth) {
                      childLeft = getPaddingLeft();
                      childTop += paddingVertical + lineHeight;
                      lineHeight = childHeight;
                  }
                  child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
                  childLeft += childWidth + paddingHorizontal;
              }
          }
          }
      

      我的 xml 是:

      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/container"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <EditText
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/editText_sample"
              />
      
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Add It"
              android:id="@+id/addit"
               />
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent">
                <!-- i mean where you put the WrapLayout class in your project that package name -->
              <your.package.name.WrapLayout
                  android:id="@+id/flow_container"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:padding="20dp"/>
          </ScrollView>
          </LinearLayout>
      

      最后在 MainActivity

              ViewGroup flowContainer;
      

      在 OnCreate 中

              flowContainer= (ViewGroup) findViewById(R.id.flow_container);
              final EditText text=(EditText)findViewById(R.id.editText_sample);
              Button addIT=(Button)findViewById(R.id.addit);
              addIT.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      String data=text.getText().toString();
                      flowContainer.addView(createTextView(data),
                              new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                  }
              });
      

      在 mainactivity 中复制这个函数

             private View createTextView(String text) {
              TextView textView = new TextView(this);
              textView.setText(text);
              textView.setGravity(Gravity.CENTER);                     
               textView.setCompoundDrawablesWithIntrinsicBounds(0,0,R.mipmap.ic_launcher,0);
              return textView;
             }
      

      【讨论】:

      • 谢谢你的回答,我试试看再回复你。
      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多