【问题标题】:How to set background color for Textview programmatically for another xml file?如何以编程方式为另一个 xml 文件设置 Textview 的背景颜色?
【发布时间】:2013-05-25 23:22:27
【问题描述】:

嗨,我是 android 新手,如何以编程方式为另一个 xml 文件设置文本背景颜色我已经使用设置内容视图添加 xml 文件,但它只有列表视图,我有另一个使用模块执行文件的 xml 文件,我想要到modelo xml文件中的文本背景

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);
    EXECUTAR = (Button) findViewById(R.id.btn_buscar);
    ValorBusca = (EditText) findViewById(R.id.txt_buscar);
    Lista = (ListView) findViewById(R.id.listView1);
    ValorBusca.setText("");
    EXECUTAR.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            QuerySQL(null);
        }
    });
}
    public  void QuerySQL(String COMANDOSQL) {
    ResultSet rs;
    try {
        Statement statement = ma.connect.createStatement();
        rs = statement.executeQuery("SELECT * FROM "+ValorBusca.getText().toString()+"");
        List<Map<String, String>> data = null;
        data = new ArrayList<Map<String, String>>();
        while(rs.next()) {
            Map<String, String> datanum =new HashMap<String, String>();
            datanum.put("A",rs.getString(1));
            datanum.put("B",rs.getString(2));
            datanum.put("c",rs.getString(3));
            data.add(datanum);  
        }

        String[] from = {"A","B","c"};
        int[] views = {R.id.txttitulo,R.id.txtconteudo,R.id.textview3};
        AD = new SimpleAdapter(this, data, R.layout.modelo, from, views);
        Lista.setAdapter(AD);

    } catch (Exception e) {
        Log.e("ERRO",e.getMessage());
        Toast.makeText(getBaseContext(),"Enter Table Name",Toast.LENGTH_SHORT).show();
    }
}

我想要这个文件模型中的文本背景

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2"
 >

 <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="2dip"
    android:scrollbarAlwaysDrawHorizontalTrack="true"

    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/txttitulo" 
        android:text="Name"
        android:layout_height="wrap_content"
        android:layout_width="2dip"
        android:gravity="left"
        android:background="@drawable/cell_shape"
        android:padding="5dip"

        android:layout_marginLeft="3dp"
        android:textColor="#0174DF"/>
    <TextView
        android:id="@+id/txtconteudo" 
        android:text="Number"
        android:layout_height="wrap_content"
        android:layout_width="2dip"
        android:gravity="left"
        android:textColor="#0174DF"
        android:background="@drawable/cell_shape"
        android:padding="5dip" 
        />
    <TextView
        android:id="@+id/textview3" 
        android:text="Number"
        android:layout_height="wrap_content"
        android:layout_width="2dip"
        android:gravity="right"
        android:layout_weight="1" 
        android:textColor="#0174DF"
        android:background="@drawable/cell_shape"
        android:padding="5dip" 
        android:layout_marginRight="3dp"/>

</TableRow> 

【问题讨论】:

  • 你想改变所有文本视图的背景颜色吗?

标签: java android performance android-layout android-intent


【解决方案1】:

检查这个,

TextView textView = (TextView) findViewById(R.id.text1);
textView.setText("Welcome");
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.RED);

【讨论】:

    【解决方案2】:

    这可能对你有帮助。

    textview.setBackgroundResource(R.color.white);
    

    注意:您需要写下您的 TextView 对象名称,而不是 textview

    【讨论】:

      【解决方案3】:

      试试这个方法。只需覆盖适配器的getView() 方法。并从那里改变颜色。

      AD = new SimpleAdapter(this, data, R.layout.modelo, from, views){
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
      View v = convertView;
      if (v == null) {
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      v = vi.inflate(R.layout.modelo, null);
      }
      TextView text1 = (TextView) v.findViewById(R.id.txttitulo);
      TextView text2 = (TextView) v.findViewById(R.id.txtconteudo);
      TextView text3 = (TextView) v.findViewById(R.id.textview3);
      text1.setTextColor(Color.GREEN);
      text2.setTextColor(Color.GREEN);
      text3.setTextColor(Color.GREEN);
      return super.getView(position, v, parent);
      }
      };
      

      希望对你有帮助。

      【讨论】:

      • 对不起,它在 android studio 中工作!!但不能在 Eclipse 中工作,它会显示创建类文件或接口
      【解决方案4】:

      试试这个

      View v=findViewById(R.layout.modelo);
      TextView tv=(TextView)v.findViewById(R.id.txttitulo);
      tv.setBackgroundColor(Color.RED);
      

      【讨论】:

        【解决方案5】:

        对于科特林;

        定义 textview globaly :

        lateinit var option1: TextView
        

        在 onCreate() 方法中初始化

        option1 = findViewById(R.id.option1)
        

        从 color.xml 设置背景颜色

        option1.setBackgroundResource(R.color.colorBlue)
        

        【讨论】:

          猜你喜欢
          • 2021-10-18
          • 2023-03-26
          • 1970-01-01
          • 2011-06-13
          • 1970-01-01
          • 2014-06-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多