简体   繁体   中英

how to give positions to textview in linearlayout in android programmatically

I created a text view using this code, I want place this text view at the top of the lineaelayout(means like header)

TextView valueTV = new TextView(Schedule.this);
                valueTV.setText("Event in Progress");
                valueTV.setTextColor(Color.parseColor("#FFFFFF"));
                valueTV.setTypeface(TypeFaceConstant.getOpensasItalic(getApplicationContext()));
                valueTV.setId(0);
                valueTV.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                valueTV.setGravity(Gravity.TOP);
                valueTV.setPadding(20, 0, 0, 0);
                lnr_list_event.addView(valueTV);

My xml file is ... I want to place above text view at first positiov(top), with using above code it placed at last... pls help me

 <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lnr_list_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical" android:paddingTop="5dp" android:paddingBottom="5dp" >

<LinearLayout
    android:id="@+id/lnr_list_progress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp" >

    <ImageView
        android:id="@+id/calender"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#00000000"
        android:src="@drawable/addevent" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="4"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/event_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:text="dfdf"
            android:textColor="#000000"
            android:textSize="18sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/event_duration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:singleLine="false"
                android:textColor="@color/event_titles"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/event_location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/event_titles"
                android:textSize="16sp" />
        </LinearLayout>

        <TextView
            android:id="@+id/event_locationss"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/event_titles"
            android:textSize="16sp"
            android:visibility="gone" />
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="5dp"
        android:src="@drawable/right_arrow" />
</LinearLayout>

It'll be better if you declare the TextView in the xml it self and hide and show it from the code. So there's no need of initializing the same object in each drawing.

Hiding and showing can be done with the below code

textView.setVisibility(View.VISIBLE); //TO Show
textView.setVisibility(View.INVISIBLE); //TO Hide
textView.setVisibility(View.GONE); //To Hide and Release the space to otherview.

In your activity get the root linearlayout by id, ie lnr_list_event. Then use method addView on the root view with index.

LinearLayout root = (LinearLayout) findViewById(R.id.lnr_list_event);

root.addView(yourHeaderView, 0, layoutparams);

Try This,

LinearLayout header = (LinearLayout)findViewById(R.id.header);

TextView valueTV = new TextView(Schedule.this);
            valueTV.setText("Event in Progress");
            valueTV.setTextColor(Color.parseColor("#FFFFFF"));
            valueTV.setTypeface(TypeFaceConstant.getOpensasItalic(getApplicationContext()));
            valueTV.setId(0);
            valueTV.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            valueTV.setGravity(Gravity.TOP);
            valueTV.setPadding(20, 0, 0, 0);
            header.addView(valueTV);

And xml file,

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lnr_list_event"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical" android:paddingTop="5dp"
    android:paddingBottom="5dp" >

<LinearLayout
    android:id="@+id/header"
    android:layout_width="wrap_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
</LinearLayout>

<LinearLayout
    android:id="@+id/lnr_list_progress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp" >

<ImageView
    android:id="@+id/calender"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:background="#00000000"
    android:src="@drawable/addevent" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_weight="4"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/event_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:text="dfdf"
        android:textColor="#000000"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/event_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="10dp"
            android:singleLine="false"
            android:textColor="@color/event_titles"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/event_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/event_titles"
            android:textSize="16sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/event_locationss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/event_titles"
        android:textSize="16sp"
        android:visibility="gone" />
</LinearLayout>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="5dp"
    android:src="@drawable/right_arrow" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM