Textview文本颜色无法以编程方式运行

时间:2017-08-18 18:04:41

标签: java android android-layout textview android-color

我正在尝试在RelativeLayout内更改文字观看次数的颜色,但出于某种原因,它无效。

enter image description here

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.TextViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.companyname.projectname.R;

import static com.companyname.projectname.R.id.FL_relativeLayout;


public class FragmentFL extends android.support.v4.app.Fragment {

    public FragmentFL() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_fl, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        RelativeLayout relativelayout = v.findViewById(FL_relativeLayout);

        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        TextView txt1 = new TextView(getActivity());
        txt1.setText("Hello world");
        TextViewCompat.setTextAppearance(txt1, android.R.style.TextAppearance_Large);
        txt1.setTextColor(Color.BLACK);


        TextView txt2 = new TextView(getActivity());
        txt2.setText("Bonjour le monde");
        TextViewCompat.setTextAppearance(txt2, android.R.style.TextAppearance_Medium);
        txt1.setTextColor(Color.BLACK);


        rlp.setMargins(0, 0, 0, 20);
        rlp.addRule(RelativeLayout.BELOW, txt1.getId());
        txt1.setLayoutParams(rlp);
        txt2.setLayoutParams(rlp);

        relativelayout.addView(txt1);
        relativelayout.addView(txt2);

        // set IDs for text views
        txt1.setId(View.generateViewId());
        txt2.setId(View.generateViewId());

        super.onActivityCreated(savedInstanceState);
    }
}

2 个答案:

答案 0 :(得分:1)

设置外观以设置文本颜色。 外观覆盖textcolor 并生成id befor set规则。

答案 1 :(得分:1)

对齐效果不佳,因为您必须在设置规则之前为视图设置Id。

<强>代码:

    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    TextView txt1 = new TextView(this);
    txt1.setText("Hello world");
    txt1.setTextColor(Color.BLACK);
    TextViewCompat.setTextAppearance(txt1, android.R.style.TextAppearance_Large);

    TextView txt2 = new TextView(this);
    txt2.setText("Bonjour le monde");
    txt2.setTextColor(Color.BLACK);
    TextViewCompat.setTextAppearance(txt2, android.R.style.TextAppearance_Medium);

    txt1.setId(View.generateViewId());
    txt2.setId(View.generateViewId());

    rlp.setMargins(0, 0, 0, 20);
    rlp.addRule(RelativeLayout.BELOW, txt1.getId());
    txt2.setLayoutParams(rlp);

    relativelayout.addView(txt1);
    relativelayout.addView(txt2);