如何在android中以编程方式创建此UI

如何在android中以编程方式创建此UI,android,imageview,layoutparams,Android,Imageview,Layoutparams,矩形是整个屏幕。虚线表示高度和宽度的中心。每条红线是imageview,绿点是imageview的中心。每个图像视图与相邻图像视图和屏幕高度相等 我正在为我的小游戏应用程序设计主菜单,该程序应该可以在所有屏幕上运行,如图所示。我浏览了许多教程,得出了以下代码,但没有得到预期的结果。在一个模拟器中,最后一个imageview不可见,且ImagView未居中。在我的三星galaxy ace中,只有第一个图像视图可见。代码有什么问题吗 package com.example.layout_test2;

矩形是整个屏幕。虚线表示高度和宽度的中心。每条红线是imageview,绿点是imageview的中心。每个图像视图与相邻图像视图和屏幕高度相等

我正在为我的小游戏应用程序设计主菜单,该程序应该可以在所有屏幕上运行,如图所示。我浏览了许多教程,得出了以下代码,但没有得到预期的结果。在一个模拟器中,最后一个imageview不可见,且ImagView未居中。在我的三星galaxy ace中,只有第一个图像视图可见。代码有什么问题吗

package com.example.layout_test2;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class MainActivity extends Activity {

int screenWidth,width_centre;
int screenHeight,first_icon_position,second_icon_position,third_icon_position,fourth_icon_position;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        LinearLayout background= new LinearLayout(this);
        background.setOrientation(LinearLayout.VERTICAL);
        background.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));


        ImageView icon_1 = new ImageView(this);
        ImageView icon_2 = new ImageView(this);
        ImageView icon_3 = new ImageView(this);
        ImageView icon_4 = new ImageView(this);

        icon_1.setScaleType(ImageView.ScaleType.MATRIX);
        icon_1.setImageResource(R.drawable.start);
        //icon_1.setOnClickListener(this);

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.start, o);
        int w1 = bmp.getWidth();
        int h1 = bmp.getHeight();

        icon_2.setScaleType(ImageView.ScaleType.MATRIX);
        icon_2.setImageResource(R.drawable.settings);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.settings, o);
        int w2 = bmp.getWidth();
        int h2 = bmp.getHeight();

        icon_3.setScaleType(ImageView.ScaleType.MATRIX);
        icon_3.setImageResource(R.drawable.more_games);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.more_games, o);
        int w3 = bmp.getWidth();
        int h3 = bmp.getHeight();


        icon_4.setScaleType(ImageView.ScaleType.MATRIX);
        icon_4.setImageResource(R.drawable.exit);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.exit, o);
        int w4 = bmp.getWidth();
        int h4 = bmp.getHeight();



        if (Build.VERSION.SDK_INT >= 17) {
            Point size = new Point();
            try {
                this.getWindowManager().getDefaultDisplay().getRealSize(size);
                screenWidth = size.x;
                screenHeight = size.y;
                System.out.println(String.valueOf(screenWidth) );
                System.out.println(String.valueOf(screenHeight));
            } catch (NoSuchMethodError e) {
                Log.i("error", "it can't work");
            }

        } else {
            DisplayMetrics metrics = new DisplayMetrics();
            this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            screenWidth = metrics.widthPixels;
            screenHeight = metrics.heightPixels;
            System.out.println(String.valueOf(screenWidth) );
            System.out.println(String.valueOf(screenHeight));


    }

        int width_centre1 = (screenWidth-w1)/2;
        int width_centre2 = (screenWidth-w2)/2;
        int width_centre3 = (screenWidth-w3)/2;
        int width_centre4 = (screenWidth-w4)/2;


        first_icon_position = (int) (screenHeight-h1)/8;
        second_icon_position = (int) 3*(screenHeight-h2)/8;
        third_icon_position = (int) 5*screenHeight/8;
        fourth_icon_position = (int) 7*screenHeight/8;


        System.out.println(screenHeight);
        System.out.println(first_icon_position);
        System.out.println(second_icon_position);
        System.out.println(third_icon_position);
        System.out.println(fourth_icon_position);

        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams1.setMargins(width_centre1,first_icon_position, 0, 0);
        background.addView(icon_1,layoutParams1);

        LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams2.setMargins(width_centre2,second_icon_position, 0, 0);
        background.addView(icon_2,layoutParams2);


        LinearLayout.LayoutParams layoutParams3  = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams3.setMargins(width_centre3,300, 0, 0);
        background.addView(icon_3,layoutParams3);


        LinearLayout.LayoutParams layoutParams4 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams4.setMargins(width_centre4,180, 0, 0);
        background.addView(icon_4,layoutParams4);


        //make visible to program
        setContentView(background);


}
}

好的,这就是我如何制作一个布局,它与您要求的布局非常相似,只是使用布局而不是代码

结果是:

240*320低密度聚乙烯屏幕

320*480 mdpi屏幕

我使用的图像:

红线,红色/可绘制/红色

中心的不可见线,res/drawable/line\u hor\u tsp

接下来的两个看起来相同,但不是

水平点模式,分辨率/可绘制/ptn_hor_blk

垂直虚线图案,分辨率/可绘制/黑色

现在,您必须将图像置于res/drawable或res/drawable mdpi(正常分辨率)中,并将其缩放以适合所有其他分辨率,请执行以下操作:

将以下两种布局放在res/drawable文件夹中:

小点:


最后但并非最不重要的一点是,如果我帮助了您,请不要忘记接受我的回答并投票。

如何使imageview彼此之间的距离相等,同时在整个屏幕上分布xml文件中的所有屏幕大小和密度?虚线仅指屏幕的中心。他们是虚构的。我只需要将四个图像视图彼此等距放置。对不起,我不太清楚。也不需要。我把它拿走了,看起来很完美。我相信布局和重力是为所有imageview提供同等权重的原因。为什么我们需要两个相对的布局?是的,这是中心的,不可见的间隔。根据你的照片,红线只在上面和下面。
<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ptn_hor_blk"
    android:tileMode="repeat"
/>
<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ptn_ver_blk"
    android:tileMode="repeat"
/>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:background="#fff"
        android:orientation="vertical"
        >
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/dots_hor"
        />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true"
            android:background="@drawable/dots_ver"
        />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            android:background="#0000"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_red"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_red"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_tsp"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_red"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_red"
            />
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:background="#fff"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            android:background="#0000"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_red"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_red"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_tsp"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_red"
            />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="@drawable/line_hor_red"
            />
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>
import android.view.Window;
import android.view.WindowManager;
@Override
protected void onCreate(
    final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Make this activity, full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Hide the Title bar of this activity screen
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.background);
}