0

Ich versuche, die Farbe meiner Textansichten innerhalb einer RelativeLayout zu ändern, aber aus irgendeinem Grund funktioniert es nicht.Textansicht Textfarbe funktioniert nicht programmatisch

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); 
    } 
} 
+0

Dieser Stil könnte die Farbe überschrieben werden: 'android.R.style.TextAppearance_Large' – Bob

+0

Und sieht aus wie Sie sind nicht' LayoutParams' für 'txt1' Einstellung. – Bob

+0

Benötige ich 'LayoutParams' für jede Textansicht? – MacaronLover

Antwort

1

Die Ausrichtung ist nicht gut, weil Sie Id für die Ansicht vor dem Festlegen der Regel festlegen müssen.

Code:

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); 
1

Set Aussehen vor dem Set Textfarbe. Die Darstellung überschreibt die Textfarbe und generiert ID vor Satzregel.

Verwandte Themen