【问题标题】:Align a View to Right of TabLayout in android在android中将视图与TabLayout的右侧对齐
【发布时间】:2016-08-30 16:45:53
【问题描述】:

我想在一个活动中显示一个 Tablayout,然后 TabLayout 想显示一个正确对齐的 ImageButton。我为此使用了以下布局,但选项卡未正确显示,并且正在裁剪最后一个选项卡。我想在 TabLayout 之后显示可滚动的选项卡和 ImageButton。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="@dimen/margin_5dp"
android:layout_marginStart="@dimen/margin_5dp"
android:layout_marginRight="@dimen/margin_5dp"
android:layout_marginEnd="@dimen/margin_5dp"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:tabMode="scrollable"
    app:tabGravity="center"
    android:background="@color/gray_background"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageButton
    android:background="@color/gray_background"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:scaleType="center"
    android:id="@+id/add_sticker_store"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginLeft="@dimen/margin_5dp"
    android:src="@drawable/ic_action_new"/>


<android.support.v4.view.ViewPager
    android:layout_below="@id/tabs"
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray_background"
    android:layout_alignParentBottom="true"/>

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_below="@id/tabs"
    android:background="@color/white"/>

请检查最后一个标签被裁剪的视图的屏幕截图。如果有人知道如何实现这一点,请提供帮助。

请帮忙,非常感谢。

【问题讨论】:

    标签: android android-relativelayout android-tablayout


    【解决方案1】:

    在 TabLayout 和 ImageButton 之间添加规则。

    <ImageButton
        android:background="@color/gray_background"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:scaleType="center"
        android:id="@+id/add_sticker_store"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="@dimen/margin_5dp"
        android:src="@drawable/ic_action_new"/>
    
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        app:tabGravity="center"
        android:background="@color/gray_background"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/add_sticker_store" />
    

    【讨论】:

    • 非常感谢您的回复,在 TabLayout 和 ImageButtin 之间添加 Rule 后,它现在可以工作了。非常感谢。 :)
    【解决方案2】:

    我认为这可以解决您的问题

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginEnd="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/ll">
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_weight="9"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:tabMode="scrollable"
                app:tabGravity="center"
                android:background="@color/colorAccent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />
    
            <ImageButton
                android:layout_weight="1"
                android:background="@color/colorAccent"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="center"
                android:id="@+id/add_sticker_store"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginLeft="5dp"/>
    
    
        </LinearLayout>
    
    
        <android.support.v4.view.ViewPager
            android:layout_below="@id/ll"
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:layout_alignParentBottom="true"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@id/ll"
            android:background="@color/colorPrimaryDark"/>
    
        </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2011-01-25
      • 2020-10-17
      • 2012-01-04
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多