Прямоугольник - это весь экран. Пунктирная линия представляет собой центр высоты и ширины. Каждая красная линия представляет собой изображение, а зеленая точка - это центр изображения. Каждое изображение равноудалено от соседнего изображения и экрана (по высоте) Как создать этот пользовательский интерфейс программно в android
Я проектирую основное меню для своего маленького игрового приложения, которое должно работать на всех экранах, как показано на рисунке. Я просмотрел множество руководств и придумал следующий код, но я не получаю ожидаемого результата. В одном эмуляторе последнее изображение не видно, а образы не центрированы. В моем туре галактики samsung видно только первое изображение. Что-то не так с кодом.?
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);
}
}
Как сделать образное изображение равноудаленным друг от друга и в то же время распространяться по всему экрану для всех размеров экрана и плотности в XML-файле? – adhithiyan