【问题标题】:Square buttons Screen Width and Weight Matching方形按钮屏幕宽度和重量匹配
【发布时间】:2016-08-03 12:38:31
【问题描述】:

我的布局只包含一个带有 3 个按钮的 LinearLayour。

我希望这 3 个按钮垂直填充屏幕,因此我使用了“weigth=1”,但我还希望按钮是方形的,即它们的宽度等于它们的高度。

有什么办法吗?

这是 .xml 文件

<?xml version="1.0" encoding="utf-8"?>

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="android.jose.se.Prueba"
    android:background="@drawable/bg"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

    <Button
        android:id="@+id/bHet"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer1"
        android:onClick="cambiarBoton"/>

    <Button
        android:id="@+id/bGay"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer2"
        android:onClick="cambiarBoton"/>

    <Button
        android:id="@+id/bLesbi"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer3"
        android:onClick="cambiarBoton"/>

</LinearLayout>
</RelativeLayout>

【问题讨论】:

标签: android xml android-layout button android-layout-weight


【解决方案1】:

我遇到了类似的问题,并使用Google's Percent Relative Layout 来帮助您管理视图纵横比。

你可以这样使用它

 <android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <Button
         android:layout_width="match_parent"
         app:layout_aspectRatio="100%"/>
 </android.support.percent.PercentFrameLayout>

【讨论】:

  • 我试过了,但它说“应该定义布局高度属性”
  • 忽略该错误,它将起作用。只需构建并尝试。我认为这是一个 lint 错误。
  • 请注意,“layout_*”属性仅用于布局。所以布局可能根本不需要“layout_height”,就像在这种情况下一样。
【解决方案2】:

最好的方法是创建一个名为 Square button 的新类,并且这个类应该扩展 Button。然后在这个类的onMeasure中,简单的把你的heightSpec设置为WidthSpec。

public class SquareButton extends Button {

public SquareButton(Context context) {
    super(context);
}

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

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

@Override
protected void onMeasure(int WidthSpec, int HeightSpec){
    int newWidthSpec = HeightSpec ;

    super.onMeasure(newWidthSpec, HeightSpec);
 }}

现在您可以像这样在您的 xml 中使用这个 SquareButton 而不是普通的 Button。 android:width 属性的值将被忽略。

<com.example.package.SquareButton
     android:height = "0dp"
     android:weight = "1"
     android:width = "0dp"
/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2013-07-17
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多