0

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;
    }
 }

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

4

2 回答 2

3

试试这样:

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;
于 2018-05-19T12:24:25.170 回答
0

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

            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);

我希望它对你有用!

于 2018-05-19T12:40:18.267 回答