如何将视图内居中的项目偏移标题的高度

发布于 2024-12-28 09:53:16 字数 1223 浏览 3 评论 0原文

我正在使用一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2025-01-04 09:53:16

您可以在底部添加一个与标题大小相同的线性布局 - 高度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

柠檬色的秋千 2025-01-04 09:53:16

我根据评论的答案是隐藏标题栏,这与此 Stackoverflow 问题中写的答案相同 如何使用现有自定义主题隐藏 XML 中活动的标题栏

我通常的方法是在自定义中执行此操作主题声明或清单。我个人认为这是一种更好的关注点分离,因为在代码中对 UI 进行的操作可以在定义的主题内完成,这看起来更整洁 - 特别是如果它必须在其他活动中复制的话。

清单示例

<activity android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

主题声明示例

<style name="generalnotitle" parent="general">
    <item name="android:windowNoTitle">true</item>
</style>

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

<activity android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

Theme Declaration Example

<style name="generalnotitle" parent="general">
    <item name="android:windowNoTitle">true</item>
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文