有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文本不会在重力=顶部时从顶部开始

我一直在尝试将工具栏的标题设置为从工具栏顶部开始,但尽管我设置了安卓:gravity="top",文本仍然从工具栏的中心(垂直)开始。我认为这可能与使用工具栏的自定义布局有关,但我之所以使用它是因为我需要它,所以我希望有人知道如何将文本设置为从顶部开始

以下是我的工具栏的布局:

<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    xmlns:app="http://schemas.安卓.com/apk/res-auto">

    <安卓.support.percent.PercentRelativeLayout
        安卓:id="@+id/layoutProjectOverview"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:layout_margin="1dp">

        <安卓.support.v7.widget.Toolbar
            安卓:id="@+id/toolbar2"
            安卓:title="@string/menuLabel1a"
            app:layout_widthPercent="37%"
            app:layout_heightPercent="26%"
            安卓:gravity="top"
            app:titleTextAppearance="@style/toolbarTitleText"
            安卓:background="@drawable/toolbar_basic"
            安卓:theme="@style/AppTheme.AppBarOverlay"
            app:popupTheme="@style/AppTheme.PopupOverlay" />


        <安卓.support.percent.PercentRelativeLayout
            安卓:id="@+id/layoutObjectNav"
            app:layout_widthPercent="63%"
            app:layout_aspectRatio="400%"
            安卓:layout_toRightOf="@id/toolbar2"
            安卓:layout_toEndOf="@id/toolbar2"
            安卓:background="@drawable/border_basic">

            <!-- Some irrelevant xml code -->

        </安卓.support.percent.PercentRelativeLayout>


        <RelativeLayout
            安卓:layout_width="match_parent"
            安卓:layout_height="match_parent"
            安卓:layout_below="@id/layoutObjectNav">

            <!-- Some more irrelevant xml code -->

        </RelativeLayout>
    </安卓.support.percent.PercentRelativeLayout>

    <!-- Some more irrelevant xml code -->

</RelativeLayout>

以下是自定义工具栏布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical">

    <TextView
        安卓:id="@+id/action_bar_title"
        安卓:textSize="20sp"
        安卓:layout_height="match_parent"
        安卓:layout_width="wrap_content"
        安卓:gravity="top"
        安卓:padding="5dp"
        安卓:ellipsize="end"
        安卓:maxLines="3"/>

</LinearLayout>

下面是我设置自定义布局(以及其他一些内容)的代码:

@Override
protected void onResume() {
    super.onResume();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
    setSupportActionBar(toolbar);
    currentProject = getIntent().getParcelableExtra("Project");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(R.layout.layout_toolbar);
        ((TextView) findViewById(R.id.action_bar_title)).setText(currentProject.getTitle());
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = (int)( size.x * 0.37);
        int height =  Math.round(size.x * 0.63f * 0.25f);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
        toolbar.setLayoutParams(params);
    } else {
        getSupportActionBar().setTitle(currentProject.getTitle());
    }
    // Some irrelevant java code
}

有人知道我做错了什么吗

编辑:

以下是我用于工具栏的文本外观样式:

<style name="toolbarTitleText">
    <item name="安卓:textSize">20sp</item>
    <item name="安卓:maxLines">3</item>
</style>

共 (2) 个答案

  1. # 1 楼答案

    尝试执行以下操作:

    1-使用getSupportActionBar().setDisplayShowTitleEnabled(false);禁用标题

    2-在toolbar下添加文本视图作为工具栏标题:

    <android.support.v7.widget.Toolbar
         android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top"/>
    
    </android.support.v7.widget.Toolbar>
    
  2. # 2 楼答案

    在工具栏中添加文本视图

    像这样:

    <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
    
        </android.support.v7.widget.Toolbar>