如何设置在线性布局中动态添加的膨胀布局中心Android
how to set inflate layout center which added dynamically in linear layout Android
我在线性布局中动态添加膨胀布局,我需要根据layouts.suppose的数量设置布局中心,如果布局是两个,那么它从中心开始显示。
以下是我的 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>
以下是我的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]);
}
}
}
我需要设置膨胀布局从中心开始。
请帮我解决this.Thank你
像这样在 xml 文件中将布局引力设置为 center_horizontal
<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>
我在线性布局中动态添加膨胀布局,我需要根据layouts.suppose的数量设置布局中心,如果布局是两个,那么它从中心开始显示。
以下是我的 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>
以下是我的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]);
}
}
}
我需要设置膨胀布局从中心开始。 请帮我解决this.Thank你
像这样在 xml 文件中将布局引力设置为 center_horizontal
<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>