Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何将imageView设置为水平居中_Android_Android Layout_View - Fatal编程技术网

Android:如何将imageView设置为水平居中

Android:如何将imageView设置为水平居中,android,android-layout,view,Android,Android Layout,View,我想在布局中水平居中放置视图。我下面的代码失败了。我怎样才能解决这个问题 请参阅我的代码: LinearLayout LLT = new LinearLayout(context); LLT.setOrientation(LinearLayout.VERTICAL); LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); //get booktheme by bo

我想在
布局中水平居中放置
视图
。我下面的代码失败了。我怎样才能解决这个问题

请参阅我的代码:

LinearLayout LLT = new LinearLayout(context);
LLT.setOrientation(LinearLayout.VERTICAL);
LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

//get booktheme by bookID 
theme = db.getthemeByID(id);
String themePath = theme.getFilepath();
int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
//imageView
ImageView imageTheme = new ImageView(context);
imageTheme.setLayoutParams(new LayoutParams(500, 700));
imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageTheme.setPadding(0, 20, 0, 10);
imageTheme.setAdjustViewBounds(true);
imageTheme.setImageResource(resid);

LLT.addView(imageTheme);
// add view 
VF.addView(LLT);

您是否尝试过使用相对布局而不是线性布局

编辑: 如果你想使用相对布局

RelativeLayout.LayoutParams layoutParams = 
    (RelativeLayout.LayoutParams)imageview.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageview.setLayoutParams(layoutParams);

(顺便说一句,我没有测试代码,但我在我的一个应用程序中使用了它,因此该应用程序可以完成此工作)

在您的
布局参数中使用
Gravity
功能,如下所示:

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
imageTheme.setLayoutParams(layoutParams);
用此代码替换您的代码,然后尝试:

LinearLayout LLT = new LinearLayout(context);
    LLT.setOrientation(LinearLayout.VERTICAL);
    LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));



    //get booktheme by bookID 
    theme = db.getthemeByID(id);
    String themePath = theme.getFilepath();
    int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
    //imageView
    ImageView imageTheme = new ImageView(context);
    LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700);
    layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
    imageTheme.setLayoutParams(layoutParams);
    imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageTheme.setPadding(0, 20, 0, 10);
    imageTheme.setAdjustViewBounds(true);
    imageTheme.setImageResource(resid);

    LLT.addView(imageTheme);
    // add view 
    VF.addView(LLT);

我已尝试将.change Linearlayout改为>>>RelativeLayout LLT=新的RelativeLayout(上下文);。一模一样put@user3001046如果你觉得我的答案有用,请投票支持。谢谢