简体   繁体   English

如何设置在线性布局Android中动态添加的充气布局中心

[英]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: 以下是我的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 像这样在xml文件中将布局重力设置为center_horizo​​ntal

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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