【问题标题】:Android: Put two elements in the center of relativeLayout programmaticallyAndroid:以编程方式将两个元素放在relativeLayout的中心
【发布时间】:2014-09-02 21:21:26
【问题描述】:

我在代码视图中添加了两个元素。一个 imageView 和一个 Spinning Wheel。显示的两个元素在同一个地方。我希望将 ImageView 放在屏幕中心的另一个元素之上。

添加视图的代码:

    RelativeLayout container= (RelativeLayout)findViewById(R.id.container);

    RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    position.addRule(RelativeLayout.CENTER_IN_PARENT);
    imgCenter.setLayoutParams(position);
    container.addView(imgCenter); 

    RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    position2.addRule(RelativeLayout.CENTER_IN_PARENT);
    position2.addRule(RelativeLayout.BELOW,imgCenter.getId());
    spinner.setLayoutParams(position2);
    container.addView(spinner);

relativelayout的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.app.exemple.LoadingActivity"
android:id="@+id/container" >

【问题讨论】:

  • 你的意思是一个应该覆盖另一个还是应该垂直排列?
  • 尝试删除这一行:position2.addRule(RelativeLayout.CENTER_IN_PARENT);
  • 如果您使用带有重力“中心”的LinearLayout而不是RelativeLayout作为父布局,那么您不需要给出任何规则等直接按顺序添加您的图像和微调器。
  • @TristanBurnside 在垂直线上。
  • @HareshChhelana 如果我 romeve 这条线,微调器显示在屏幕顶部,我希望它显示在 imageView 的底部。

标签: android android-layout android-relativelayout


【解决方案1】:

在第二个孩子的父母中移除中心

RelativeLayout container= (RelativeLayout)findViewById(R.id.container);

RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.addRule(RelativeLayout.CENTER_IN_PARENT);
imgCenter.setLayoutParams(position);
container.addView(imgCenter); 

RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position2.addRule(RelativeLayout.BELOW,imgCenter.getId());
spinner.setLayoutParams(position2);
container.addView(spinner);

【讨论】:

    【解决方案2】:

    感谢@HareshChhelana,我找到了解决方案。 我将xml更改如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.app.exemple.LoadingActivity"
    android:id="@+id/container"
    android:orientation="vertical"
    android:gravity="center" >
    

    还有代码:

    LinearLayout container= (LinearLayout)findViewById(R.id.container);
        ProgressBar spinner = new ProgressBar(this,null,android.R.attr.progressBarStyleLarge);
        container.addView(imgCenter); 
        container.addView(spinner);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多