【问题标题】:How to load a Custom Java View class to a RelativeLayout programmatically如何以编程方式将自定义 Java 视图类加载到 RelativeLayout
【发布时间】:2018-01-22 10:32:44
【问题描述】:

我想在我的RelativeLayout 中加载一些自定义视图,但不知道如何操作。我尝试过的代码不起作用,因此有人知道如何正确执行此操作吗?

XML

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin">

        <TextView
            android:id="@+id/textView0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginBottom="20dp"
            style="@android:style/TextAppearance.Medium" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginBottom="2dp"
            style="@android:style/TextAppearance.Medium"
            android:layout_below="@id/textView0" />
        <View
            android:id="@+id/drawing0"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_below="@id/textView1" />
        <View
            android:id="@+id/drawing1"                
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="2dp"
            android:layout_below="@id/drawing0" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Large"
            android:layout_below="@id/drawing1" />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Large"
            android:layout_marginStart="10dp"
            android:layout_below="@id/drawing1"
            android:layout_toEndOf="@id/textView2" />
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Medium"
            android:layout_below="@id/textView3" />
        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Medium"
            android:layout_marginBottom="30dp"
            android:layout_below="@id/textView4" />
    </RelativeLayout>

</ScrollView>

Shapes.java

public class Shapes extends android.support.v4.app.Fragment {

    public Shapes() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.shapes, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        //blue shape drawing
        View cv0 = (View)v.findViewById(R.id.drawing0);
        v.addView(new BlueShape(getActivity()));

        //green shape drawing
        View cv1 = (View)v.findViewById(R.id.drawing1);
        v.addView(new GreenShape(getActivity()));

        super.onActivityCreated(savedInstanceState);
    }
}

BlueShape.java

public class BlueShape extends View {
    private final Paint mBlue = new Paint();

    ...

}

GreenShape.java

public class GreenShape extends View {
    private final Paint mGreen = new Paint();

    ...

}

【问题讨论】:

  • 有什么问题?
  • 错误出现在哪里?

标签: java android xml android-relativelayout android-paint


【解决方案1】:

参考https://developer.android.com/guide/topics/ui/custom-components.html

特别是在

修改现有视图类型

  1. 使用自定义组件

你可以改变你的RelativeLayout中的xml

<View... 

<your.package.GreenShape...

如果您事先不知道视图将是什么,则将其更改为

<FrameLayout... instead of <View...

然后以编程方式将子项(GreenShape 或 BlueShape)添加到该框架布局

【讨论】:

  • 我已经这样做了,但需要重用布局来显示许多其他不同的视图,因此这样做不是一个好主意。
  • @MacaronLover 将drawing0和drawing1从View更改为FrameLayout有效吗?您需要一个布局来包含子视图。 BlueShape/GreenShape 的宽度和高度也可能为 0dp(因此没有显示)
【解决方案2】:

cv0、cv1 不是 ViewGroup。 尝试将 cv0、cv1 改为 ViewGroup 并将 onActivityCreated 的代码移动到 onViewCreated 回调方法中。

这里是参考网站。 android/view/ViewGroup

【讨论】:

    【解决方案3】:

    我认为问题在于您使用的是v.addView() 而不是

    cv0.addView()
    

    但我建议使用以下内容而不是您的View

     <LinearLayout
                android:id="@+id/drawing1"                
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="2dp"
                android:layout_below="@id/drawing0" />
    

    然后,在 Java 代码中:

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        LinearLayout cv0 = (LinearLayout)getView().findViewById(R.id.drawing0);
        cv0.addView(new BlueShape(getActivity()), 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      相关资源
      最近更新 更多