2017-08-14 39 views
0

我想在我的RelativeLayout中加載一些自定義視圖,但不知道如何。我試過的代碼不起作用,因此沒有人知道如何正確執行此操作?如何以編程方式將自定義Java視圖類加載到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(); 

    ... 

} 
+0

問題是什麼? – Vyacheslav

+0

哪裏出現錯誤? – Vyacheslav

回答

1

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

具體地說下

修改現有視圖類型

  • 使用定製組件
  • 可以更改RelativeLayout中的xml

    <View... 
    

    <your.package.GreenShape... 
    

    ,如果你不事先知道哪些搜索將是,然後將其更改爲

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

    ,然後添加子(GreenShape或BlueShape)編程到該框架佈局

    +0

    我已經這樣做了,但需要重新使用佈局來顯示其他幾十個不同的視圖,因此這樣做並不是一個好主意。 – MacaronLover

    +0

    @MacaronLover \t 是否將drawing0和drawing1從View更改爲FrameLayout?你需要一個佈局來包含子視圖。此外,BlueShape/GreenShape的寬度和高度可能爲0dp(因此它不會顯示) –

    1

    cv0,cv1不是ViewGroup。 嘗試將cv0,cv1更改爲ViewGroup,並將onActivityCreated的代碼移至onViewCreated回調方法。

    這裏是參考網站。 android/view/ViewGroup

    1

    我覺得現在的問題是,你正在使用的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)); 
    
    +0

    @MacaronLover錯誤的代碼。我已經修復了 – Vyacheslav

    相關問題