如何将视图内居中的项目偏移标题的高度
我正在使用一个简单的 Android 视图(2 个按钮)。我已将按钮垂直居中,但我注意到它们比直接居中稍微向下一点。
据我所知,它们正在使用剩余的空间居中(在标题放入视图之后)。我更喜欢的是标题不是视图的一部分,或者我可以通过特定的倾斜来偏移居中(因此它看起来居中而不是偏移看起来)
任何人都知道这是否确实是我的问题或者您是否可以偏移居中物品?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/loadViewBtn"
android:layout_width="200dip"
android:layout_height="80dip"
android:text="View"
android:textSize="18sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</Button>
<Button
android:id="@+id/loadUploadBtn"
android:layout_width="200dip"
android:layout_height="80dip"
android:text="Upload"
android:textSize="18sp"
android:layout_below="@+id/loadViewBtn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</Button>
</RelativeLayout>
I'm working with a simple Android view (2 buttons). I've centered the buttons vertically but I noticed they are a little further down than directly center.
From what I can tell they are being centered using the space left over (after the header is put into the view). What I would prefer is that the header is not part of the view or that I could offset the centering by a specific dip (so it looks center instead of offset looking)
Anyone know if this is truly my issue or if you can offset a centered item?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/loadViewBtn"
android:layout_width="200dip"
android:layout_height="80dip"
android:text="View"
android:textSize="18sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</Button>
<Button
android:id="@+id/loadUploadBtn"
android:layout_width="200dip"
android:layout_height="80dip"
android:text="Upload"
android:textSize="18sp"
android:layout_below="@+id/loadViewBtn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</Button>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在底部添加一个与标题大小相同的线性布局 - 高度Android 中的状态栏
或删除标题,请参阅 http://www.androidsnippets.com/how-to-make-an-activity-fullscreen
You could add a linearlayout on the bottom that is the same size as the header -- Height of status bar in Android
Or remove the header, see http://www.androidsnippets.com/how-to-make-an-activity-fullscreen
我根据评论的答案是隐藏标题栏,这与此 Stackoverflow 问题中写的答案相同 如何使用现有自定义主题隐藏 XML 中活动的标题栏
我通常的方法是在自定义中执行此操作主题声明或清单。我个人认为这是一种更好的关注点分离,因为在代码中对 UI 进行的操作可以在定义的主题内完成,这看起来更整洁 - 特别是如果它必须在其他活动中复制的话。
清单示例
主题声明示例
My answer based upon comments is to hide the title bar, which is the same answers written in this Stackoverflow question How to hide the title bar for an Activity in XML with existing custom theme
My usual method to do this is either in the custom theme declaration OR manifest. I personally believe this is a better separation of concerns, as doing things to the UI in code that could be done within a defined theme just seems neater - especially if it has to be replicated in other activities.
Manifest Example
Theme Declaration Example