【问题标题】:Center text in EditText not working programmatically when adding background resource添加背景资源时,EditText 中的居中文本无法以编程方式工作
【发布时间】:2013-12-18 22:59:15
【问题描述】:

我正在开发一个以编程方式将 EditText 框插入到布局中的应用程序。当我以编程方式添加 EditText 时,我似乎无法使中心文本正常工作,但当我通过 XML 布局进行时它工作正常。我已经确定了添加背景资源时的问题。我在下面添加了代码,以显示通过代码添加时的 EditText 和 XML。

<LinearLayout 
   android:id="@+id/layoutLinear_main"
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:background="@drawable/background" >             

   <LinearLayout 
      android:id="@+id/layoutLinear_answerDisplay"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:gravity="center_horizontal"       
      android:orientation="vertical" >

      <EditText 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingLeft="5dp"
         android:paddingRight="5dp"
         android:paddingTop="5dp"
         android:paddingBottom="5dp"
         android:gravity="center"
         android:background="@drawable/background_field"            
         android:layout_gravity="center"
         android:layout_marginLeft="120dp"
         android:layout_marginRight="120dp"
         android:layout_marginTop="10dp"
         android:layout_marginBottom="10dp"
         android:textSize="30dp"
         android:hint="F"
         android:text="F" />

  </LinearLayout></LinearLayout>

然后在主代码中。我以编程方式添加了一个 EditText。我删除了边距和填充,只是为了清楚地识别问题。

LinearLayout layoutLinear_answerDisplay = ((LinearLayout) this.findViewById(R.id.layoutLinear_answerDisplay));

EditText ed = new EditText(this);               
ed.setGravity(Gravity.CENTER);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;        
ed.setLayoutParams(layoutParams);

ed.setBackgroundResource(R.drawable.background_field);
ed.setTextSize(30);
ed.setHint("F");
ed.setText("F");

layoutLinear_answerDisplay.addView(ed);

以下是输出。您可以从图像中看到顶部的 EditText 具有居中的文本(这是通过 XML 代码完成的)。您可以从底部的 EditText 看到,文本偏离中心(通过后面的代码完成)。

我还为 EditText 附加了我的背景资源。任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    看起来您正在使用 layout_margin(左和右)来居中 xml 编辑文本。当您在代码中设置这些时,您没有考虑 dp 缩放。

        final int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                (float) 120.0, getResources().getDisplayMetrics());
    

    使用类似上述的方法将 120 度量缩放为正确的像素等效值,然后它们将位于同一个位置。但也要意识到,你不应该需要使用如此高的左右边距值来让你的文本居中

    【讨论】:

    • 嗨@dangVarmit,在阅读了您的评论后,我决定删除对边距和填充的依赖,看看这是否可行。在测试代​​码时,我已经确定了添加免费资源时的问题(即 ed.setBackgroundResource(R.drawable.background_field))。您知道这会影响文本居中的任何原因吗?我将更新我的主要问题以代表我的发现
    • 定义了填充的九个补丁可以改变居中。填充定义为底部和右侧像素边缘的黑线
    • 非常感谢。这就是问题所在。这是由于黑线。我把它们放在九个补丁图像的右侧,所以我只是把它们放在两边,它工作得很好。非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多