mardi 26 mai 2015

Append TextView to RelativeLayout

I have a RelativeLayout containing a TextView. I want to append a new TextView under the existing TextView programmatically in the onCreateView method, but what happens when I try to do so, is that the TextView I add programmatically gets overlapped with the existing one. More details below:

public static class ContentTours extends Fragment{
    public static String TOUR_NAME= "tour_info";
    View view = getView();
    TextView contentTour;RelativeLayout toursView;
    public ContentTours(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tours_view, container, false);

        toursView = (RelativeLayout) view.findViewById(R.id.rel_layout);
        contentTour = (TextView) view.findViewById(R.id.tours);

        contentTour.setText(getArguments().getString("2"));

        ViewGroup.LayoutParams stt = contentTour.getLayoutParams();

        TextView nt = new TextView(getActivity().getBaseContext());

        nt.setText(getArguments().getString("1"));

        toursView.addView(nt, new ViewGroup.MarginLayoutParams(stt));
        return view;
    }
}

I have a RelativeLayout containing the TextView. tours_view.xml:

<RelativeLayout android:id="@+id/rel_layout"     
xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:layout_marginBottom="16dp">

    <TextView android:id="@+id/tours" android:clickable="true"
        android:background="?android:selectableItemBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:text="Lorem ipsum"/>

</RelativeLayout>

the reason why I have a Fragment class is that tours_view.xml is a layout that I am applying to another FrameLayout.

Aucun commentaire:

Enregistrer un commentaire