在Android按键宽度等于不同的文本的宽度(不是按钮的文本) [英] Make button width equal DIFFERENT text width (not the button's text) on Android

查看:151
本文介绍了在Android按键宽度等于不同的文本的宽度(不是按钮的文本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的登录按钮,比我的应用程序名称的文字更广泛,因为它使用了相对布局的固定填充,但我希望它会自动扩大与应用程序名称文本的左边和右边边排队。有没有什么方法以编程方式做到这一点?

Right now my Log In button is wider than my app name text because it uses fixed padding of the relative layout, but I want it to automatically widen to line up with the left and right edges of the app name text. Is there any way to programmatically do this?

我希望它看起来相同的所有设备。我担心如果我只是硬code我自己的空间,这将右看看我的设备上,但可能不右看看在其他设备上,如果他们提供的字体不同。

I want it to look the same on all devices. I'm afraid that if I just hard-code my own margins, it will look right on my device, but might not look right on other devices if they render the fonts differently.

谢谢!

layout.xml:

layout.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:animateLayoutChanges="true"
android:background="@drawable/back_image"
android:padding="20dp" >

    <TextView
    android:id="@+id/AppNameTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/margin_top"
    android:fontFamily="Arial"
    android:text="@string/app_title"
    android:textColor="@color/white"
    android:textSize="@dimen/text_size" />

    <com.timey.widget.Button
    android:id="@+id/signInButton"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:background="@color/green"
    android:fontFamily="Arial"
    android:text="@string/sign_in"
    android:textColor="@color/white"
    android:textSize="30sp" />

</RelativeLayout>

样机用红色虚线说明我多么希望应用程序名称的两侧与我的登录按钮的两侧排队:

Mockup with red dash lines illustrating how I want the sides of the app name to line up with the sides of my log-in button:

推荐答案

我不能完全肯定这是否会有所帮助,但让我试试看。

I am not entirely sure if this would help, but let me give it a try.

<?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="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="App\nTitle" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/title"
        android:layout_alignRight="@id/title"
        android:layout_below="@id/title"
        android:text="Log In" />

</RelativeLayout>

然而,该按钮将是一样宽的TextView的,它可能没有足够的宽以显示其自己的文本

However, the button would be just as wide as the textview, it may not be wide enough to display its own text.

这篇关于在Android按键宽度等于不同的文本的宽度(不是按钮的文本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆