【问题标题】:android correct padding between views programmaticallyandroid以编程方式在视图之间正确填充
【发布时间】:2015-11-06 10:20:15
【问题描述】:

我以编程方式创建了四个 Textviews 并将其添加到我的 Linearlayout.my linearlayout 具有 216dp 宽度TextView 我以编程方式创建的具有 48 dp 宽度和高度。 我想在TextView 之间添加填充。我写了一些代码,但我收到了这个结果

这是我的代码

 for (int i = 0; i < mDigits; i++) {
        DigitView digitView = new DigitView(getContext());

        digitView.setWidth(valueInPixels);//48dp
        digitView.setHeight(valueInPixels);//48dp
        digitView.setTextColor(Color.WHITE);
        digitView.setTextSize(mDigitTextSize);
        digitView.setGravity(Gravity.CENTER);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            digitView.setElevation(mDigitElevation);
        }

        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if (i > 0) {
            lp.leftMargin = 10;//problem is this line
        }
        childView.addView(digitView, lp);//childview 216dp
    }

如何在视图programmatically 之间创建正确的填充? 如果有人有解决方案请帮助我 谢谢。

【问题讨论】:

  • 为您的动态文本字段添加边距并删除此部分:LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); if (i > 0) { lp.leftMargin = 10;//问题是这一行 }
  • @TdSoft 我不知道该怎么做。你能告诉我源代码或更新我的代码吗?
  • @Nanoc 感谢。但我知道要添加填充,但正如我所说,我的容器有 216 dp 宽度,在我的视图中有 48dp 宽度,简单我希望我的视图之间有相同的填充。请看我的图片
  • @donoachua 请检查我的答案。

标签: android android-linearlayout textview android-layoutparams


【解决方案1】:

首先将适当的 android:paddingLeft 添加到您添加的子视图中,如下所示:

<LinearLayout
        android:id="@+id/childView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:background="#000"
        android:orientation="horizontal">

然后编辑你的代码如下

for (int i = 0; i < 4; i++) {
            DigitView digitView = new DigitView(this);

            digitView.setWidth(valueInPixels);//48dp
            digitView.setHeight(valueInPixels);//48dp
            digitView.setTextColor(Color.WHITE);
            digitView.setBackgroundColor(Color.YELLOW);
            digitView.setTextSize(mDigitTextSize);
            digitView.setGravity(Gravity.CENTER);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                digitView.setElevation(mDigitElevation);
            }

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                            lp.rightMargin = 10;//problem is this line

            childView.addView(digitView, lp);//childview 216dp
        }

【讨论】:

  • 谢谢,但 5dp 和 rightMargin meybe 不是正确的解决方案,因为不同的屏幕尺寸 meybe 会出现问题
  • 使用 values/dimen.xml 定义不同屏幕尺寸的填充尺寸。
【解决方案2】:

Add padding on view programmatically

这里有在 java 代码中创建填充的解决方案。

【讨论】:

  • thanks.but 我知道要添加填充,但正如我所说,我的容器有 216 dp 宽度,在我的视图中有 48dp 宽度,简单我希望我的视图之间有相同的填充。请看我的图片跨度>
  • 使用 values/dimen.xml 定义不同屏幕尺寸的停车尺寸
【解决方案3】:

试试这个,

public void setPadding (int left, int top, int right, int bottom)

view.setPadding(0,padding,0,0);

【讨论】:

  • thanks.but 我知道要添加填充,但正如我所说,我的容器有 216 dp 宽度,在我的视图中有 48dp 宽度,简单我想要在我的视图之间使用相同的填充。请看我的图片@Parth巴哈尼
  • 为此,您需要为每个视图赋予权重并将该视图添加到您的活动中,这样可以正常工作。
猜你喜欢
  • 2016-01-11
  • 2012-06-12
  • 2012-03-29
  • 1970-01-01
  • 2017-04-07
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多