代码之家  ›  专栏  ›  技术社区  ›  Shawn.Y

防止文本视图与相对布局中的下一个元素重叠

  •  0
  • Shawn.Y  · 技术社区  · 8 年前

    我正在创建一个标题栏,如下图所示,“主页”和“抽屉”图标将根据案例设置可见/消失。 如何使文本视图不与“主页”/“抽屉”图标重叠??


    enter image description here

    这就是我的代码,我手动将文本视图边距设置为“主页”和“抽屉”图标,但是,当“主页/抽屉”图标被隐藏时,文本“long still maintenance like home”按钮会出现。如何根据可用空间设置文本视图的完美换行?

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <ImageButton
            android:id="@+id/actionbar_backbutton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="?attr/actionBarItemBackground"
            android:scaleType="centerInside"
            android:src="@drawable/ic_arrow_back_black_24dp" />
    
        <TextView
            android:id="@+id/actionbar_title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="105dp"
            android:layout_toRightOf="@id/actionbar_backbutton"
            android:background="?attr/actionBarItemBackground"
            android:ellipsize="end"
            android:gravity="center"
            android:maxLines="1"
            android:text="@string/Easybook_com"
            android:textColor="@color/colorBlackText"
            android:textSize="20sp" />
    
        <ImageButton
            android:id="@+id/actionbar_homebutton"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/actionbar_drawerbutton"
            android:layout_alignParentTop="true"
            android:background="?attr/actionBarItemBackground"
            android:scaleType="centerInside"
            android:src="@drawable/ic_home_black" />
    
        <ImageButton
            android:id="@+id/actionbar_drawerbutton"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="?attr/actionBarItemBackground"
            android:scaleType="centerInside"
            android:src="@drawable/ic_drawer" />
    
        <ImageView
            android:id="@+id/actionbar_reddot"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="?attr/actionBarItemBackground"
            android:scaleType="centerInside" />
    
    5 回复  |  直到 8 年前
        1
  •  0
  •   seyed Jafari    8 年前

    您可以将TextView设置为两个按钮之间的空间,如下所示:

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageButton
        android:id="@+id/actionbar_backbutton"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="?attr/actionBarItemBackground"
        android:scaleType="centerInside"
        android:src="@drawable/ic_arrow_back_black_24dp" />
    
    <ImageButton
        android:id="@+id/actionbar_homebutton"
        android:layout_width="?attr/actionBarSize"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/actionbar_drawerbutton"
        android:layout_alignParentTop="true"
        android:background="?attr/actionBarItemBackground"
        android:scaleType="centerInside"
        android:src="@drawable/ic_home_black" />
    
    <ImageButton
        android:id="@+id/actionbar_drawerbutton"
        android:layout_width="?attr/actionBarSize"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="?attr/actionBarItemBackground"
        android:scaleType="centerInside"
        android:src="@drawable/ic_drawer" />
    
    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="105dp"
        android:layout_toRightOf="@id/actionbar_backbutton"
        android:layout_toLeftOf="@id/actionbar_homebutton"
        android:background="?attr/actionBarItemBackground"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        android:text="@string/Easybook_com"
        android:textColor="@color/colorBlackText"
        android:textSize="20sp" />
    
    </RelativeLayout>
    
        2
  •  0
  •   Sahil    8 年前

    android:layout_toRightOf="@id/actionbar_backbutton"
    android:layout_toStartOf="@id/actionbar_backbutton"
    android:layout_toLeftOf="@id/actionbar_homebutton"
    android:layout_toEndOf="@id/actionbar_homebutton"
    
        3
  •  0
  •   Drashti Joshi    8 年前

    试试这个。。

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
    
        <ImageButton
            android:id="@+id/actionbar_backbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/actionBarItemBackground"
            android:scaleType="centerInside"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_arrow_back_black_24dp" />
    
        <TextView
            android:id="@+id/actionbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="?attr/actionBarItemBackground"
            android:ellipsize="end"
            android:gravity="center"
            android:maxLines="1"
            android:layout_toRightOf="@id/actionbar_backbutton"
            android:text="@string/Easybook_com"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="20sp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_toRightOf="@+id/actionbar_title"
            android:orientation="horizontal">
    
            <ImageButton
                android:id="@+id/actionbar_homebutton"
                android:layout_width="?attr/actionBarSize"
                android:layout_height="wrap_content"
                android:background="?attr/actionBarItemBackground"
                android:scaleType="centerInside"
                android:src="@drawable/ic_home_black" />
    
            <ImageButton
                android:id="@+id/actionbar_drawerbutton"
                android:layout_width="?attr/actionBarSize"
                android:layout_height="wrap_content"
                android:background="?attr/actionBarItemBackground"
                android:scaleType="centerInside"
                android:src="@drawable/ic_drawer" />
    
            <ImageView
                android:id="@+id/actionbar_reddot"
                android:layout_width="?attr/actionBarSize"
                android:layout_height="wrap_content"
                android:background="?attr/actionBarItemBackground"
                android:scaleType="centerInside" />
        </LinearLayout>
    
    
    </RelativeLayout>
    
        4
  •  0
  •   Jay Mungara    8 年前

    我觉得你让事情变得更复杂了。您也可以使用这样的方法来执行您的需求,您只需与活动xml无关。

    现在,在您的活动onCreate中:

    setTitle("Your Title");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    然后,实现菜单的这两个默认方法,

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_view, menu);
    return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            break;
        case R.id. actionbar_drawerbutton:
            //your code
            break;
        case R.id. actionbar_reddot:
        //your code
        break;
        }
    return super.onOptionsItemSelected(item);
    }
    

    res/menu/menu_view.xml 这样地,

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <item
            android:title="Drawer Button"
            android:id="@+id/actionbar_drawerbutton"
            android:icon="@drawable/ic_menu_camera"
            app:showAsAction="always">
    </item>
    <item
            android:title="Red Dot"
            android:id="@+id/actionbar_reddot"
            android:icon="@drawable/ic_favorite"
            app:showAsAction="always">
    </item>
    
    </menu>
    

    这会处理你所有的案子。试试这个。我相信你会得到更好的产量。

        5
  •  0
  •   prashant17 OzanTabakoglu    8 年前

    替换TextView和 use startOf and EndOf instead of LeftOf and rightOf

    <TextView
            android:id="@+id/actionbar_title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="105dp"
            android:layout_toRightOf="@id/actionbar_backbutton"
            android:layout_toLeftOf="@id/actionbar_homebutton"
            android:background="?attr/actionBarItemBackground"
            android:ellipsize="end"
            android:gravity="center"
            android:maxLines="1"
            android:text="@string/Easybook_com"
            android:textColor="@color/colorBlackText"
            android:textSize="20sp" />