获取机器人屏幕的尺寸
Basically I'm trying to make an application for tablets. There going to be a bunch of widgets added and removed by the user inside a table layout. It needs to be dynamic so that the widgets are sized differently based on the width and height of the table layout.
基本上我正在尝试为平板电脑申请。在表格布局中,用户会添加和删除一堆小部件。它需要是动态的,以便根据表格布局的宽度和高度来调整窗口小部件的大小。
To do this, I'm trying to make a 3x3 grid in a tablelayout. As I'm sure you guessed, each square has 33% width and height of the tablelayout.
为此,我试图在tablelayout中创建一个3x3网格。我敢肯定你猜对了,每个方格的宽度和高度都是桌面布局的33%。
This is what I got:
这就是我得到的:
DisplayMetrics metrics;
int totalScreenSizeH;
int totalScreenSizeW;
TableLayout contentTable;
LinearLayout contentLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Get the UI detail
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
totalScreenSizeH = metrics.heightPixels;
totalScreenSizeW = metrics.widthPixels;
contentTable=(TableLayout)findViewById(R.id.tableLayout1);
contentLayout=(LinearLayout)findViewById(R.id.contentLayout);
int tableWidth =contentTable.getWidth();
int tableHeight=contentTable.getHeight();
int layoutWidth=contentLayout.getWidth();
int layoutHeight=contentLayout.getHeight();
Log.v("Table Width",""+tableWidth);
Log.v("Table Height",""+tableHeight);
Log.v("Layout Width",""+layoutWidth);
Log.v("Layout Height",""+layoutHeight);
So just to explain this a bit better. The linearLayout contains the tablelayout which has 3 table rows. I figured that I would be able to get the width and height of these but for some reason, both are coming back as 0 in size. One I have the width and height dims, Im going to save that variable and the create the widgets based on those variables/3.
所以只是为了更好地解释这一点。 linearLayout包含tablelayout,它有3个表行。我想我能够得到这些的宽度和高度,但由于某种原因,两者都回来了0的大小。一个我有宽度和高度调暗,我将保存该变量,并根据这些变量/ 3创建小部件。
So 2 questions:
那2个问题:
1: is this the right way to handle dynamically sized widgets. Or is this the wrong way to handle a problem like this?
1:这是处理动态大小的小部件的正确方法。或者这是处理这样的问题的错误方法吗?
2: Any ideas why those 2-4 variables are all coming back as 0?
2:为什么这2-4个变量都回归为0的任何想法?
2 个解决方案
#1
1
I would advise against doing pixel-perfect designs on android, due to the vast amount of resolutions and densities you have to deal with.
由于你需要处理大量的分辨率和密度,我建议不要在android上做像素完美的设计。
My suggestion would be to look at android:layout_width/height="wrap_content"
and android:layout_weight="INTEGER"
that will help you make floating 3x3. You can also achieve this with a GridView
我的建议是看看android:layout_width / height =“wrap_content”和android:layout_weight =“INTEGER”,这将帮助你制作浮动3x3。您也可以使用GridView实现此目的