【问题标题】:My Toolbar compound view doesn't align with parent view group properly我的工具栏复合视图未与父视图组正确对齐
【发布时间】:2017-02-14 16:21:59
【问题描述】:

我需要为我正在编写的新应用程序提供自定义工具栏。当我尝试将视图添加到我的主布局时,它没有正确对齐。谁能解释一下原因?

public class LjCustomToolbar extends Toolbar {

private Context context;
private LayoutInflater layoutInflater;
private TextView toolbar_title;

public LjCustomToolbar(Context context) {
    super(context);
    initialize(context);
}

public LjCustomToolbar(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context);
}


public LjCustomToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize(context);
}

private void initialize(Context context) {
    this.context = context;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.custom_toolbar, this);
}

}

custom_toolbar.xml

<Toolbar
style="@style/LjToolBarDefault"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Profile"
    style="@style/LjBodyNormalText"
    android:textColor="@color/white"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title_text" />

 </Toolbar>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="@color/lj_color_primary">

<us.lj.CustomViews.LjCustomToolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>

Click here to see my current layout

【问题讨论】:

    标签: java android android-custom-view android-toolbar android-viewgroup


    【解决方案1】:

    你能显示一个屏幕截图,看看有什么问题吗?
    为什么要自定义工具栏?如果你想在工具栏中添加另一个视图,你可以在 xml 中像所有其他 UI 元素一样进行操作

    【讨论】:

    • 我对不同活动的工具栏有不同的要求。就像自定义颜色一样,我也需要能够控制后退按钮的可见性。请点击问题末尾的链接查看图片。
    • 首先,用 AppBarLayout 包围您的工具栏。其次,要自定义颜色,请改用主题。
    猜你喜欢
    • 2014-09-28
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多