首页 > 解决方案 > 从代码将图像对齐到中心 LinearLayout

问题描述

wix在我的 react-native 应用程序中使用 react-native-navigation,所以为了在我的应用程序中显示启动屏幕,我必须在这个类的代码中编写 android 布局。

由于我没有在 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;
    }
 }

如果有人可以帮助我弄清楚如何实现这一目标。谢谢。

标签: androidandroid-layoutreact-native-androidwix-react-native-navigation

解决方案


试试这样:

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;

推荐阅读