Android 如何创建类似菜单栏的布局?
在我的一项活动中,我希望有一个类似“菜单栏”的东西,在纵向模式下位于屏幕底部,在横向模式下位于屏幕右侧 菜单栏是永久可见的,将包含图像按钮,这些按钮具有相同的高度,但可能具有不同的宽度。根据“菜单栏”中“按钮”的数量选择适当的填充,以便正确使用可用高度或witdh。 基本上应该是这样的: 正如建议的那样,我已经考虑过使用“分割动作栏”。但在这种情况下,我只在纵向模式下具有所需的效果Android 如何创建类似菜单栏的布局?,android,layout,menu,menubar,Android,Layout,Menu,Menubar,在我的一项活动中,我希望有一个类似“菜单栏”的东西,在纵向模式下位于屏幕底部,在横向模式下位于屏幕右侧 菜单栏是永久可见的,将包含图像按钮,这些按钮具有相同的高度,但可能具有不同的宽度。根据“菜单栏”中“按钮”的数量选择适当的填充,以便正确使用可用高度或witdh。 基本上应该是这样的: 正如建议的那样,我已经考虑过使用“分割动作栏”。但在这种情况下,我只在纵向模式下具有所需的效果 目前我考虑实现自定义GRIDVIEW布局以获得此效果,或者修改找到的仪表板模式。 也许你们中的一些人已经实现了类
目前我考虑实现自定义GRIDVIEW布局以获得此效果,或者修改找到的仪表板模式。
也许你们中的一些人已经实现了类似“菜单栏式布局”的东西,可以为我指出正确的方向 编辑 我稍微修改了上面提到的仪表板模式代码,这只是第一次概念验证,但似乎达到了我的目的:public class Nx1Layout extends ViewGroup {
private static final String TAG = "Nx1Layout";
private int mMaxChildWidth = 0;
private int mMaxChildHeight = 0;
private int mScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
public Nx1Layout(Context context) {
super(context, null);
}
public Nx1Layout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public Nx1Layout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setScreenOrientation(int screenOrientation) {
mScreenOrientation = screenOrientation;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mMaxChildWidth = 0;
mMaxChildHeight = 0;
// Measure once to find the maximum child size.
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.AT_MOST);
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
mMaxChildWidth = Math.max(mMaxChildWidth, child.getMeasuredWidth());
mMaxChildHeight = Math.max(mMaxChildHeight, child.getMeasuredHeight());
}
// Measure again for each child to be exactly the same size.
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxChildWidth, MeasureSpec.EXACTLY);
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxChildHeight, MeasureSpec.EXACTLY);
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
setMeasuredDimension(resolveSize(mMaxChildWidth, widthMeasureSpec),resolveSize(mMaxChildHeight, heightMeasureSpec));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = r - l;
int height = b - t;
final int count = getChildCount();
// Calculate the number of visible children.
int visibleCount = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
++visibleCount;
}
if (visibleCount == 0) {
return;
}
// Horizontal and vertical space between items
int hSpace = 0;
int vSpace = 0;
int cols, rows;
if(mScreenOrientation == Configuration.ORIENTATION_PORTRAIT) {
cols = visibleCount;
rows = 1;
} else {
cols = 1;
rows = visibleCount;
}
hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));
vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));
// Lay out children based on calculated best-fit number of rows and cols.
// If we chose a layout that has negative horizontal or vertical space, force it to zero.
hSpace = Math.max(0, hSpace);
vSpace = Math.max(0, vSpace);
// Re-use width/height variables to be child width/height.
width = (width - hSpace * (cols + 1)) / cols;
height = (height - vSpace * (rows + 1)) / rows;
int left, top;
int col, row;
int visibleIndex = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
row = visibleIndex / cols;
col = visibleIndex % cols;
left = hSpace * (col + 1) + width * col;
top = vSpace * (row + 1) + height * row;
child.layout(left, top, (hSpace == 0 && col == cols - 1) ? r : (left + width), (vSpace == 0 && row == rows - 1) ? b : (top + height));
++visibleIndex;
}
}
}
public类Nx1Layout扩展了视图组{
私有静态最终字符串TAG=“Nx1Layout”;
private int mMaxChildWidth=0;
私有整数mMaxChildHeight=0;
private int mScreenOrientation=Configuration.ORIENTATION\u纵向;
公共Nx1Layout(上下文){
super(上下文,null);
}
公共Nx1Layout(上下文、属性集属性){
超级(上下文,属性,0);
}
公共Nx1Layout(上下文、属性集属性、int defStyle){
超级(上下文、属性、定义样式);
}
公共无效设置屏幕方向(int屏幕方向){
mScreenOrientation=屏幕方向;
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
mMaxChildWidth=0;
mMaxChildHeight=0;
//测量一次以找到最大子大小。
int childwidthmasurespec=MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthmasurespec),最多MeasureSpec;
int childheightmeasurept=measurept.makemeasurept(measurept.getSize(heightmeasurept),measurept.AT_max);
最终整数计数=getChildCount();
for(int i=0;i
下面是一些用于测试它的活动代码:
public class TestActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
// setup ui
super.onCreate(savedInstanceState);
setContentView(R.layout.ui_test);
Nx1Layout layout = (Nx1Layout)findViewById(R.id.layout1);
layout.setScreenOrientation(getScreenOrientation());
}
public int getScreenOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if(getOrient.getWidth() == getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
} else{
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
}}
公共类测试活动扩展了SherlockFragmentActivity{
@凌驾
创建时的公共void(Bundle savedInstanceState){
//设置界面
super.onCreate(savedInstanceState);
setContentView(R.layout.ui_测试);
NX1布局=(NX1布局)findViewById(R.id.layout1);
layout.setScreenOrientation(getScreenOrientation());
}
public int getScreenOrientation()
{
显示getOrient=getWindowManager().getDefaultDisplay();
int方向=配置。方向\未定义;
如果(getOrient.getWidth()==getOrient.getHeight()){
方向=配置。方向\方形;
}否则{
if(getOrient.getWidth()
这是布局图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.yourpackagename.Nx1Layout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/layout1">
<ImageView
android:id="@+id/image1"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="some description"
android:scaleType="centerInside"
android:padding="2dp"
android:src="@drawable/test_draw" />
<ImageView
android:id="@+id/image2"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="some description"
android:scaleType="centerInside"
android:padding="2dp"
android:src="@drawable/test_draw" />
<ImageView
android:id="@+id/image3"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="some description"
android:scaleType="centerInside"
android:padding="2dp"
android:src="@drawable/test_draw" />
</com.yourpackagename.Nx1Layout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.yourpackagename.Nx1Layout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="70dp"
android:layout_height="match_parent"
android:id="@+id/layout1">
<ImageView
android:id="@+id/image1"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="some description"
android:scaleType="centerInside"
android:padding="2dp"
android:src="@drawable/test_draw" />
<ImageView
android:id="@+id/image2"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="some description"
android:scaleType="centerInside"
android:padding="2dp"
android:src="@drawable/test_draw_narrow" />
<ImageView
android:id="@+id/image3"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="some description"
android:scaleType="centerInside"
android:padding="2dp"
android:src="@drawable/test_draw" />
</com.yourpackagename.Nx1Layout>
</LinearLayout>