简体   繁体   中英

how to set inflate layout center which added dynamically in linear layout Android

I am adding inflate layout dynamically in linear layout,i need to set layout center based on number of layouts.suppose if layouts are two , then it start showing from center.

following is my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<HorizontalScrollView
    android:id="@+id/hsv_category_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:scrollbars="none">

    <LinearLayout
        android:id="@+id/ll_placeHolder"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:orientation="horizontal">

    </LinearLayout>

</HorizontalScrollView>

</RelativeLayout>

and following are my class

public class ScrollTest extends Activity {

HorizontalScrollView hsv_category_list;
LinearLayout ll_placeHolder;
View layoutView[];

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scroll_test);
    hsv_category_list = (HorizontalScrollView) 
   findViewById(R.id.hsv_category_list);
    ll_placeHolder = (LinearLayout) findViewById(R.id.ll_placeHolder);
    layoutView = new View[2];

    for (int i = 0; i < 2; i++) {
        layoutView[i] = 
     LayoutInflater.from(this).inflate(R.layout.category_list_item, null);
        layoutView[i].setId(i);

        View parent = layoutView[i].findViewById(i);
        TextView tvTime = (TextView) 
   parent.findViewById(R.id.tv_arrival_time);
        tvTime.setText("" + i);

        ll_placeHolder.setGravity(Gravity.CENTER);
        ll_placeHolder.addView(layoutView[i]);
    }

   }
}

输出

i need to set the inflate layout start from center. Please help me to solve this.Thank you.

Set layout gravity to center_horizontal in xml file like this

<LinearLayout
android:id="@+id/ll_placeHolder"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:orientation="horizontal">
</LinearLayout>

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