将图像对齐到代码中的 LinearLayout 中心

Align image to to center LinearLayout from code

我在我的 react-native 应用程序中使用来自 wix 的 react-native-navigation,因此为了在我的应用程序中显示启动画面,我必须在此代码中编写 android 布局class.

因为我没有在 android 中工作,所以我无法在 LinearLayout 中将图像和一些文本居中对齐。

这是模型的样子。

这是我到目前为止编写的代码。

import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ImageView;
import com.reactnativenavigation.controllers.SplashActivity;

 public class MainActivity extends SplashActivity {
    @Override
    public LinearLayout createSplashLayout() {
        LinearLayout view = new LinearLayout(this);
        FrameLayout frame = new FrameLayout(this);
        ImageView imageView = new ImageView(this);
        ImageView logoView = new ImageView(this);
        logoView.setImageResource(R.mipmap.icon);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
        logoView.setLayoutParams(layoutParams);
        logoView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setImageResource(R.drawable.welcome);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        frame.addView(imageView);
        frame.addView(logoView);
        view.addView(frame);
        return view;
    }
 }

如果有人能帮我弄清楚如何实现这一点。 谢谢

这样试试:

LinearLayout linearLayout = new LinearLayout(this);
    LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    linearLayout.setLayoutParams(llParams);
    RelativeLayout view = new RelativeLayout(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout
            .LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
    view.setLayoutParams(params);

    ImageView bgView = new ImageView(this);
    RelativeLayout.LayoutParams bgParam =
            new RelativeLayout.LayoutParams(RelativeLayout
                    .LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
    bgView.setLayoutParams(bgParam);
    bgView.setImageResource(R.drawable.welcome);
    bgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.addView(bgView);

    ImageView logoView = new ImageView(this);
    RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(RelativeLayout
            .LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    logoView.setLayoutParams(layoutParams);
    logoView.setImageResource(R.mipmap.icon);
    view.addView(logoView);
    linearLayout.addView(view);
    return linearLayout;

您可以尝试此解决方案以将图像设置在中心并设置布局背景图像。

            LinearLayout view = new LinearLayout(this); 
            view.setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.example_appwidget_preview));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
            params.gravity = Gravity.CENTER;
            view.setLayoutParams(params);
            ImageView logoView = new ImageView(this);
            logoView.setImageResource(R.mipmap.ic_launcher);
            view.addView(logoView);

希望对你有用!