【问题标题】:Text won't start at top with gravity=top文本不会以重力=顶部开头
【发布时间】:2016-05-30 13:42:46
【问题描述】:

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

这是我的工具栏所在的布局:

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

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

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


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

            <!-- Some irrelevant xml code -->

        </android.support.percent.PercentRelativeLayout>


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

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

        </RelativeLayout>
    </android.support.percent.PercentRelativeLayout>

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

</RelativeLayout>

这里是自定义工具栏布局代码:

<?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">

    <TextView
        android:id="@+id/action_bar_title"
        android:textSize="20sp"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:gravity="top"
        android:padding="5dp"
        android:ellipsize="end"
        android: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
}

有人知道我做错了吗?

编辑:

这是我用于工具栏的 TextAppearance 样式:

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

【问题讨论】:

  • 工具栏的大小、textview的textsize和工具栏中的padding,是否还有空间让textview重新定位?
  • 我会这么认为,但我不确定
  • 您是否在此处覆盖了您的风格中的重力:app:titleTextAppearance="@style/toolbarTitleText" ?
  • @BHuelse 不,我没有。另外我刚刚将样式添加到问题中

标签: java android xml textview android-toolbar


【解决方案1】:

在工具栏中添加一个 textView

像这样:

<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>

【讨论】:

【解决方案2】:

尝试执行以下操作:

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

2- 在 toolbar 下添加一个 textView 作为工具栏标题:

<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>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
相关资源
最近更新 更多