android - 对齐屏幕中央两个按钮之间的文本,其中一个按钮可能不可见

标签 android android-layout

这很难解释,请耐心等待。

我在别人给我的设计中有一个顶部菜单栏(它是一个端口)。

菜单栏有两个按钮,一个在菜单的左边,一个在右边,两个按钮之间有一个文本标题。

TextView 是单行的,所以如果文本比 View 长,它会在末尾用省略号缩短。文本应在按钮之间的显示宽度内居中。文本是当前内容的标题,可以缩短它。实际上,在大多数设备上,只有一两个奇数字母被切掉。

如果两个按钮都可见,那么它非常简单,但是,根据我在应用程序中的位置,右侧的按钮可能不可见。当它不可见时,我使用 View.GONE。问题是文本现在填充了所需的剩余空间,但居中于剩余空间,而不是在显示宽度内,因此一小段文本向右偏移了第一个按钮的宽度。

使用 View.INVISIBLE 而不是 View.GONE 不是答案,因为文本会正确居中,但不会使用可用空间来最小化文本的缩短。

这是我想要达到的效果:

两个按钮可见:

---------------------------------------------------------------
| Left button |  Title text centred in display | Right button |
---------------------------------------------------------------

隐藏右键

---------------------------------------------------------------
| Left button |  Title text centred in display                |
---------------------------------------------------------------

隐藏长文本的右键按钮显示省略号。这就是我要找的。

---------------------------------------------------------------
| Left button |  A really long piece of title text centred ...|
---------------------------------------------------------------

这是我隐藏右键时得到的结果,请注意文本没有居中。

---------------------------------------------------------------
| Left button |        Title text centred in this space       |
---------------------------------------------------------------

这是我目前使用的布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/activitytitlegradient"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"    
    android:gravity="center_vertical">

   <ImageView android:id="@+id/mainMenuLeftButton" 
                android:background="#00ffffff"
                android:layout_gravity="center_vertical"        
            android:src="@drawable/rr_boat_bow"
            android:scaleType="fitStart"        
            android:layout_weight="1"   
            android:layout_height="34dip"
            android:layout_width="wrap_content"/>

   <TextView  android:id="@+id/mainMenuTitle"
                    android:layout_weight="10"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"        
            android:paddingLeft="2dp"
            android:background="#00ffffff"          
            android:text="Reference"
            android:singleLine="true"
            android:textSize="19sp"
            android:textStyle="bold" 
            android:textColor="#ffffff"     
            android:gravity="center_horizontal|center_vertical" />


    <Button android:id="@+id/mainMenuRightButton" 
            android:background="@drawable/buttonborder"
            android:text="Edit"     
            android:textSize="13sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:layout_margin="3dip"
            android:padding="3dip"
            android:layout_weight="1"                   
            android:layout_height="35dip"
            android:layout_width="wrap_content"
            android:visibility="gone"/>


</LinearLayout>

第一个按钮作为 ImageView 实现,以启用我在其他地方做的其他一些(不相关的)效果。文本是硬编码的,只是为了帮助布局。我的代码根据需要更改文本。

我可以使用自定义 View 执行此操作,但我确信必须有一种方法可以使用标准框架执行此操作。

感谢您提供任何线索...

最佳答案

使用 RelativeLayout 而不是 LinearLayout

<RelativeLayout 
    android:background="@drawable/activitytitlegradient"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:gravity="center_vertical">
    <ImageView android:id="@+id/mainMenuLeftButton" 
        android:background="#00ffffff"
        android:layout_gravity="center_vertical"        
        android:src="@drawable/rr_boat_bow"
        android:scaleType="fitStart"        
        android:layout_alignParentLeft="true"  
        android:layout_height="34dip"
        android:layout_width="wrap_content"/>
    <TextView  android:id="@+id/mainMenuTitle"
        android:layout_width="wrap_content"  // or fiexed like 400dp
        android:layout_height="wrap_content"        
        android:paddingLeft="2dp"
        android:background="#00ffffff"          
        android:text="Reference"
        android:singleLine="true"
        android:textSize="19sp"
        android:textStyle="bold" 
        android:textColor="#ffffff"     
        android:layout_toRightOf="@id/mainMenuLeftButton"
        android:gravity="center_horizontal|center_vertical" />
    <Button android:id="@+id/mainMenuRightButton" 
        android:background="@drawable/buttonborder"
        android:text="Edit"     
        android:textSize="13sp"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:layout_margin="3dip"
        android:padding="3dip"
        android:alignParentRight="true"
        android:layout_height="35dip"
        android:layout_width="wrap_content"
        android:visibility="invisible"/>
</RelativeLayout>

看看它是否有效,或者你可以添加一些规则,如 android:layout_xxxxx,它与 RelativeLayout 更灵活。

编辑:invisible & toRightOf 在评论中回复

关于android - 对齐屏幕中央两个按钮之间的文本,其中一个按钮可能不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9051661/

相关文章:

java - 如何在 Android(Java) 中使用 json 反斜杠

android - 如何使 fcm 推送通知过期或设置生命周期?

android - Filter.filter()的Filter.Filterlistener如何获取结果?

android - 如何在jetpack Compose中居中按钮?

android - Android 中的相对布局

android - 以编程方式创建 View 时,如何知道要使用哪个 LayoutParams 类?

Android Studio 2.3.1 按钮在模拟器或实际设备中的位置不正确

android - 在RectF Android的交叉点填充透明

android - View 不包含卡片 View 中的内容

android - 使用 Android 应用程序的 Facebook 登录错误