【问题标题】:Align Spinner Text to left when set background on Spinner在 Spinner 上设置背景时将 Spinner 文本左对齐
【发布时间】:2013-06-01 07:57:23
【问题描述】:

我在 xml 文件中有一个微调器,宽度和高度设置为 wrap_content,我将背景设置为微调器,

文本显示在中心,如果我在 dp 或 fill_parent 中设置宽度,则文本左对齐显示,但当微调器宽度为 wrap_content 时我需要左对齐

【问题讨论】:

  • 您可能需要附加 9-patch 文件和布局。
  • 请发布您的 xml 文件

标签: android android-spinner


【解决方案1】:

你可以这样使用 将您的 Spinner 视图设置为:

res/layout/my_spinner_textview.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="?android:attr/spinnerItemStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />

    public class HelloSpinner extends Activity
    {
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array,
    //        android.R.layout.simple_spinner_item);
            R.layout.my_spinner_textview);
    //    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(R.layout.my_spinner_textview);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
      }
  //No other modification needed.

【讨论】:

    【解决方案2】:

    试试这个:

    android:paddingRight="10dp"
    OR
    android:paddingLeft="10dp"
    

    根据需要设置大小。这将为微调器视图的文本设置填充。

    【讨论】:

    • 您说的是哪种尺寸?微调器的宽度和高度?
    • 我将微调器的宽度和高度设置为 wrap_content,因为我将背景设置为微调器
    【解决方案3】:

    在您的自定义文本视图中包含这四行,以使您的微调器文本左对齐。

    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2016-01-16
      相关资源
      最近更新 更多