gpt4 book ai didi

java - 相对布局中的 Textview 不会以编程方式出现在彼此下方

转载 作者:行者123 更新时间:2023-12-02 02:39:14 26 4
gpt4 key购买 nike

我试图将一些 TextView 放置在 RelativeLayout 中的另一个 TextView 下方,但由于某种原因它不起作用:

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

// add text views
TextView txt1 = new TextView(getActivity());
txt1.setText("Blue");
TextViewCompat.setTextAppearance(txt1, android.R.style.TextAppearance_Large);
txt1.setTextColor(Color.BLACK);

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

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

TextView txt4 = new TextView(getActivity());
txt4.setText("Red");
TextViewCompat.setTextAppearance(txt4, android.R.style.TextAppearance_Medium);
txt4.setTextColor(Color.BLACK);

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

TextView txt6 = new TextView(getActivity());
txt6.setText("White");
TextViewCompat.setTextAppearance(txt6, android.R.style.TextAppearance_Medium);
txt6.setTextColor(Color.BLACK);

txt1.setId(View.generateViewId());
txt2.setId(View.generateViewId());
txt3.setId(View.generateViewId());
txt4.setId(View.generateViewId());
txt5.setId(View.generateViewId());
txt6.setId(View.generateViewId());

rlp.addRule(RelativeLayout.BELOW, txt1.getId());
rlp.addRule(RelativeLayout.BELOW, txt2.getId());
rlp.addRule(RelativeLayout.BELOW, txt3.getId());
rlp.addRule(RelativeLayout.BELOW, txt4.getId());
rlp.addRule(RelativeLayout.BELOW, txt5.getId());
txt2.setLayoutParams(rlp);
txt3.setLayoutParams(rlp);
txt4.setLayoutParams(rlp);
txt5.setLayoutParams(rlp);
txt6.setLayoutParams(rlp);

relativelayout.addView(txt1);
relativelayout.addView(txt2);
relativelayout.addView(txt3);
relativelayout.addView(txt4);
relativelayout.addView(txt5);
relativelayout.addView(txt6);


super.onActivityCreated(savedInstanceState);
}
}

最佳答案

您需要为每个 TextView 使用不同的 LayoutParams,如

 rlp.addRule(RelativeLayout.BELOW, txt1.getId());
rlp1.addRule(RelativeLayout.BELOW, txt2.getId());
rlp2.addRule(RelativeLayout.BELOW, txt3.getId());
rlp3.addRule(RelativeLayout.BELOW, txt4.getId());
rlp4.addRule(RelativeLayout.BELOW, txt5.getId());

txt2.setLayoutParams(rlp);
txt3.setLayoutParams(rlp1);
txt4.setLayoutParams(rlp2);
txt5.setLayoutParams(rlp3);
txt6.setLayoutParams(rlp4);

关于java - 相对布局中的 Textview 不会以编程方式出现在彼此下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45763190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com