2

我是韩国人,下图是韩语。嗯..这个事实并不重要。我尝试在线性布局中添加自定义视图,并且它成功地处于线性布局中。但它不在中心水平线上。我确实将线性布局中心的重力设置为水平。但这行不通。我猜问题的原因是布局参数。自定义视图类没有问题,调试时也没有出现任何错误。我该怎么办??

XML 代码中的活动:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:ignore="NestedScrolling">
    <LinearLayout
        android:id="@+id/select_body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        <!--Here is-->
    </LinearLayout>
</ScrollView>

添加视图方法:

ThemeManager mTheme;

Button selectTopBoardButton;

LinearLayout selectBody;

ThemeView c0001;
ThemeView c0002;
ThemeView c0003;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select);

    gestureDetectorCompat = new GestureDetectorCompat(this, new MainActivityGoer());

    NetworkDetector networkDetector = new NetworkDetector(this);
    if (!networkDetector.isConnected()) {
        Toast.makeText(SelectActivity.this, "네트워크에 접속할 수 없습니다.", Toast.LENGTH_SHORT).show();
    }

    selectTopBoardButton = (Button) findViewById(R.id.select_top_board_button);
    selectBody = (LinearLayout) findViewById(R.id.select_body);

    mTheme = new ThemeManager(this);
    c0001 = mTheme.getThemeView("c0001");
    c0002 = mTheme.getThemeView("c0002");
    c0003 = mTheme.getThemeView("c0003");

    selectTopBoardButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url = getResources().getString(R.string.main_request_btn06_url);
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    });

    selectBody.addView(c0001);
}
4

2 回答 2

1

尝试为这样的自定义视图设置重力

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
c0001.setLayoutParams(params); //your custom ThemeView
于 2018-02-13T11:13:12.203 回答
1

我认为您的子视图 layout_width="match_parent" 请尝试此代码

c0001.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

selectBody.addView(c0001); 
于 2018-02-13T11:47:33.973 回答