【问题标题】:How do I make a custom LinearLayout?如何制作自定义 LinearLayout?
【发布时间】:2017-12-23 10:55:39
【问题描述】:

我需要制作一个带有标题的 LinearLayout。有点像这样 https://imgur.com/Kwa7a47
有谁知道怎么做这样的吗?

编辑: 这是我设置为布局背景的边框可绘制对象

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<corners
    android:topRightRadius="30dp"
    android:bottomLeftRadius="30dp" />
<stroke
    android:width="2dp"
    android:color="#000000"/>
</shape>

【问题讨论】:

  • 您的图片无法加载,但我在下面发布了答案。希望对你有用。
  • 对此很抱歉,但基本上我正在制作一个带有 TextView 的 LinearLayout。 TextView 的位置在边框之上。这是一个新链接ibb.co/jCDC26
  • 我更改了一些可能有用的东西。您必须创建一个可绘制的 xml 文件并设置为 Linearlayout 的背景。

标签: java android android-linearlayout


【解决方案1】:

您可以创建一个从 Linerlayout 扩展的类。然后在 onCreate 方法中,你会膨胀一个 xml 布局。

public class ValueSelector extends Linerlayout {

View rootView;
TextView valueTextView;

public ValueSelector(Context context) {
    super(context);
    init(context);
}

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

private void init(Context context) {
    //do setup work here
    rootView = inflate(context, R.layout.your_custom_view, this);
}
}

然后在your_layout_view.xml 中编写您的自定义视图,例如:

<LinearLayout 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:orientation="vertical"
android:background="@drwable.boarder_bd"
tools:context=".LinearLayout" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Data"
            android:paddingTop="10px"/>
</LinearLayout>

【讨论】:

  • 谢谢您的回答,但我需要的是标题必须在边框的顶部,就像我发送的图片一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多