【问题标题】:What layout in android should be used to achieve the following buttons?android中应该使用什么布局来实现以下按钮?
【发布时间】:2016-03-01 23:05:13
【问题描述】:

我已经为 twitter 和 facebook 构建了一个混合应用程序,它具有以下格式的按钮:

我正在尝试重新构建 Android 中的两个按钮,但我之间总是有一个差距,而且我总是不得不弄乱 dp 以将它们组合在一起。

有没有一种好方法可以让 Android 中的按钮如图所示?

只需要一些建议。

谢谢

【问题讨论】:

  • @Nant 看看我的回答,它将满足您的需求
  • @Tony 我今天晚上会去看。非常感谢感谢

标签: android facebook button


【解决方案1】:

一种可能的解决方案是使用 TextView 而不是 Button,将背景设置为 XML 选择器(如果需要,您可以创建一个只有一侧圆角的矩形)。在 textview 上将边距设置为零,以便它们接触。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/your_drawable_here"
        android:clickable="true"
        android:gravity="center"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="Left button" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/your_drawable_here"
        android:clickable="true"
        android:gravity="center"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="Right button" />

</LinearLayout>

【讨论】:

  • 我会使用图像视图(它们是图像)但这是答案 - 如果您不希望它看起来像按钮,请不要使用按钮。
  • @Francesc 如何使文本视图从左右变小?
  • 您可以添加左右边距。将左边距添加到左侧文本视图,将右边距添加到右侧文本视图。或者为文本视图(线性布局)的容器添加填充。
【解决方案2】:

这是背景图

     <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#fff"/>

    <corners
        android:bottomLeftRadius="3dp"
        android:bottomRightRadius="3dp"
        android:topLeftRadius="3dp"
        android:topRightRadius="3dp"/>

    </shape>

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#199fd4"/>

    <corners
        android:bottomRightRadius="3dp"
        android:topRightRadius="3dp"/>

</shape>

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#1f19d4"/>

    <corners
        android:bottomLeftRadius="3dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="3dp"
        android:topRightRadius="0dp"/>

</shape>

然后您需要创建线性布局并将两个按钮放置在其中,每个按钮各占 50%,您可以使用 weight 属性进行设置

布局代码

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:background="@drawable/roundedallsides_white"
    android:weightSum="2">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/rounded_left"></RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/rounded_right"></RelativeLayout>
</LinearLayout>

在这两个相对布局中,你可以放置任何你想要的东西,然后在它们上设置点击监听器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多