【问题标题】:Set margin in style and apply that style to TextView programmatically在样式中设置边距并以编程方式将该样式应用于 TextView
【发布时间】:2012-03-17 21:14:45
【问题描述】:

在我的应用程序中,我想将 8 dip 的顶部和底部边距设置为 textview。所以如果我这样做 -

<TextView
android:id="@+id/tv_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/settings_plain_text"/>

在样式内容所在的地方可以正常工作-

<style name="settings_plain_text">
    <item name="android:layout_marginTop"> 8dip </item>
    <item name="android:layout_marginBottom"> 8dip </item>
    <item name="android:textSize"> 18sp </item>
</style>

但是当我以编程方式将相同的样式应用于该文本视图时 -

textview.setTextAppearance(context, R.style.settings_plain_text);

它没有显示我在样式中设置的顶部和底部边距。 请帮忙。

【问题讨论】:

  • android set style in code 的可能副本; setTextAppearance() 仅顾名思义更改文本外观,不包括边距。设置 View 的样式更复杂,并且在我在这里提到的问题中作为重复项链接(因为它基本上是相同的基础问题)。
  • @corsair992 您实际上应该将该问题评论为可能重复,因为该问题是在该问题之前 3 年提出的。 :)
  • 样式是在视图膨胀期间应用的(非常简化),因此您所要求的全部内容是不可能的。
  • @Rajkiran:另一个问题更笼统,并提供了一些潜在的建议。请参阅Opinions on closing an older question as a duplicate of a newer question 上的元答案

标签: android android-styles


【解决方案1】:

setTextAppearence(..) 仅设置带有前缀 android:text_* 的 xml 属性,但您仍然可以编写自己的方法来读取其他属性并以编程方式设置它们,了解底层的 LayoutParams 实现。

请注意,您可以使用ContextThemeWrapper 来获取特定主题值或在.obtainStyledAttributes(.. 中传递样式资源ID)

举个例子:

int[] attrs= new int[] {
  android.R.attr.layout_marginTop,     // 0
  android.R.attr.layout_marginLeft,    // 1
  android.R.attr.layout_marginRight,   // 2 (used in example)
  android.R.attr.layout_marginBottom}; // 3
final TypedArray arr = context.obtainStyledAttributes(attrs);
try {
  // ...
  layoutParams.rightMargin = arr.getDimensionPixelSize(2);
  // ...
} finally {
  arr.recycle();
}

【讨论】:

    【解决方案2】:

    Gianluca P 回答的更简化版本。

    public void applyTextStyle(TextView v, int styleResId)
    {
        int[] attrs= new int[] {
                android.R.attr.layout_marginTop,     // 0
                android.R.attr.layout_marginLeft,    // 1
                android.R.attr.layout_marginBottom,   // 2 
                android.R.attr.layout_marginRight}; // 3 (used in example)
        final TypedArray arr =  this.obtainStyledAttributes(styleResId,attrs);
        try {
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            lp.topMargin = arr.getDimensionPixelSize(0,lp.topMargin);
            lp.leftMargin = arr.getDimensionPixelSize(1,lp.leftMargin);
            lp.bottomMargin = arr.getDimensionPixelSize(2,lp.bottomMargin);
            lp.rightMargin = arr.getDimensionPixelSize(3,lp.rightMargin);
    
            v.requestLayout();
        } finally {
            arr.recycle();
        }
    }
    

    【讨论】:

      【解决方案3】:
      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(textView.getLayoutParams());
          lp.setMargins(10, 20, 10, 20);
          textView.setLayoutParams(lp);
      

      //在xml中我的textView的父布局是RelativeLayout。

      <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
         <TextView
          android:id="@+id/test"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@color/bg_gradient_end"
          android:text="zdfhzfdh"/>
      </RelativeLayout
      

      【讨论】:

        【解决方案4】:

        您可以使用此代码来实现它。

        TextView tv = (TextView)findViewById(R.id.tv_text1);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
        params.setMargins(0, 8, 0, 8); //substitute parameters for left, top, right, bottom
        tv.setLayoutParams(params);
        

        希望对你有帮助!

        编辑:

        public final class Values {
            public static final int MARGIN_TOP = 8;
            public static final int MARGIN_BOTTOM = 8;
        }
        
        TextView tv = (TextView)findViewById(R.id.tv_text1);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
        params.setMargins(0, Values.MARGIN_TOP, 0, Values.MARGIN_BOTTOM); //substitute parameters for left, top, right, bottom
        tv.setLayoutParams(params);
        

        【讨论】:

        • 请完整阅读问题。这个问题清楚地表明它需要样式边距。所以我不必一直为每个 textView 定义它。附言如果边距应该更改为 10 像素而不是 8 像素,您的答案会怎样?我应该找到所有的线条并在任何地方进行更改吗? :)
        • @Rajkiran 您可以通过使用这些值创建一个静态类来获取它。如果要更改值,只需在 Values 类中更改即可
        • 期待这个答案。其余的款式呢?您不能(即使您这样做,也不应该)在 java 文件而不是 XML 中执行所有 UI 内容。风格最适合。不过感谢您的回答。
        • @Rajkiran:你问的只是这个......我回答你如何解决它。这是一个有效的答案。您选择使用或不使用它。祝你好运!
        猜你喜欢
        • 2011-03-09
        • 1970-01-01
        • 2013-02-07
        • 1970-01-01
        • 2016-04-24
        • 2019-02-23
        • 2017-03-11
        • 2012-04-12
        • 1970-01-01
        相关资源
        最近更新 更多