【问题标题】:Center TextView in custom ActionBar horizontally在自定义 ActionBar 中水平居中 TextView
【发布时间】:2016-11-10 20:17:49
【问题描述】:

想要创建自定义 ActionBar,应用名称位于其中心,但仍与左侧对齐。这是 custom_actionbar.xml

的 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:background="@color/transparent">

        <TextView
            android:id="@+id/nav_bar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@color/transparent"
            android:singleLine="true"
            android:textColor="@color/sue_green"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceLarge">
        </TextView>

</RelativeLayout>

这里是关于创建 Activity:

public class DeviceOverviewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

        setTheme(R.style.Theme_SUE);
        setContentView(R.layout.activity_device_overview);

        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);

        LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.custom_actionbar, null);

        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(v);
    }
...
}

你能告诉我我做错了什么吗? 谢谢

【问题讨论】:

  • 尝试使用 LinearLayout,并删除此行 android:layout_centerInParent="true"

标签: android android-layout textview


【解决方案1】:

将TextViews 宽度更改为match_parent 并应用android:gravity="center_horizontal"

【讨论】:

    【解决方案2】:

    使用android支持ToolBar自定义actionbar..

    将此链接用于Tool bar reference

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 2022-01-19
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2014-03-23
      • 1970-01-01
      • 2013-12-28
      相关资源
      最近更新 更多