0

我在线性布局中动态添加充气布局,我需要根据布局数量设置布局中心。如果布局为两个,则需要设置布局中心,然后从中心开始显示。如何设置在线性布局中动态添加的充气布局中心Android

以下是我的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> 

和下面是我的课

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]); 
    } 

    } 
} 

output

我需要设置从中心充气布局的开始。 请帮我解决这个问题,谢谢。

回答

1

组布局重心在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> 
相关问题