无法将线性布局居中,因此无法将imageView置于其中心

时间:2015-07-05 20:17:58

标签: android android-layout

我想动态创建一个ImageView并将其放在中心的中心。我尝试用xml做,我能够。我试图将相同的东西转换为代码,但imageView始终出现在屏幕的左上角。有人能指出错误是什么吗?我还是Android的新手并找到了自己的方式。也可能存在失误。

由于

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
    android:id="@+id/myimg"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:scaleType="fitXY"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/exmp"
    />
</LinearLayout>

代码

 public class MainActivity extends ActionBarActivity {

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

    Resources res = getResources();
    String pluginPackageName = this.getApplicationContext().getPackageName();

    int anim = res.getIdentifier("anim_android", "anim",pluginPackageName);
    int transparent_style = res.getIdentifier("TransparentProgressDialog", "style", pluginPackageName);
    int drawable_spinner = res.getIdentifier("exmp", "drawable", pluginPackageName);

    LinearLayout layout = new LinearLayout(getApplicationContext());

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

    layout.setLayoutParams(layoutParams);
    layout.setGravity(Gravity.CENTER);
    layout.setOrientation(LinearLayout.VERTICAL);

    LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100));
    imageViewParams.gravity = Gravity.CENTER_HORIZONTAL;

    ImageView iv = new ImageView(getApplicationContext());
    iv.setLayoutParams(imageViewParams);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);

    iv.setImageResource(drawable_spinner);

    layout.addView(iv, imageViewParams);
    addContentView(layout, imageViewParams);
}

4 个答案:

答案 0 :(得分:1)

要完全像在XML布局文件中那样,您必须将addContentView(layout, imageViewParams);更改为setContentView(layout);

编辑: 完整代码

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

    Resources res = getResources();
    String pluginPackageName = this.getApplicationContext().getPackageName();

    //int anim = res.getIdentifier("anim_android", "anim",pluginPackageName);
    //int transparent_style = res.getIdentifier("TransparentProgressDialog", "style", pluginPackageName);
    //int drawable_spinner = res.getIdentifier("exmp", "drawable", pluginPackageName);

    LinearLayout layout = new LinearLayout(getApplicationContext());

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

    layout.setLayoutParams(layoutParams);
    layout.setGravity(Gravity.CENTER);
    layout.setOrientation(LinearLayout.VERTICAL);

    LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100));
    imageViewParams.gravity = Gravity.CENTER_HORIZONTAL;

    ImageView iv = new ImageView(getApplicationContext());
    iv.setLayoutParams(imageViewParams);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);

    iv.setImageResource(R.mipmap.ic_launcher);

    layout.addView(iv, imageViewParams);
    setContentView(layout);
}

答案 1 :(得分:0)

你有这个作为onCreate的最后一行:

addContentView(layout, imageViewParams);

也许你是这个意思?

addContentView(layout, layoutParams);

或者,也许这个:

setContentView(layout, layoutParams);

答案 2 :(得分:0)

使用layoutGravity引入重力。

imageViewParams.layoutGravity = Gravity.CENTER_HORIZONTAL;

答案 3 :(得分:0)

您已设置layout_gravity。你可以这样做:

LinearLayout layout = new LinearLayout(getApplicationContext());
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

            layoutParams.gravity = Gravity.CENTER;

            layout.setLayoutParams(layoutParams);
            layout.setGravity(Gravity.CENTER);
            layout.setOrientation(LinearLayout.VERTICAL);