2016-10-15 4 views
-1

프로그래밍 방식으로 RelativeLayout을 기존 xml 레이아웃에 추가 할 수 없습니다. XML 레이아웃 만 표시됩니다. 내 코드 공유프로그래밍 방식으로 RelativeLayout을 추가 할 수 없습니다.

fragment_home.xml :

<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" 
tools:context="com.timetable.act.MainActivity$PlaceholderFragment" 
android:id="@+id/homeLayout" 
android:background="@color/white"> 


<ImageButton 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:layout_marginTop="380dp" 
    android:layout_marginLeft="240dp" 
    android:background="@drawable/circle" 
    android:id="@+id/addEvent" 
    android:src="@drawable/ic_mode_edit_white_24dp" 
    android:layout_gravity="right|bottom" 
    android:contentDescription="@string/hello" /> 
</RelativeLayout> 

PlaceholderFragment.java

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = null; 

     switch (Integer.valueOf(getArguments().get(ARG_SECTION_NUMBER).toString())) { 
     case 1: 
      rootView = inflater.inflate(R.layout.fragment_home, container, false); 
      displyAllEvent(rootView); 

      break; 
     case 2: 
      rootView = inflater.inflate(R.layout.fragment_setting, container, false); 
      break; 
     default: 
      rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      break; 
     } 
     return rootView; 
    } 
private void displyAllEvent(View mainView){ 
     Drawable drawableShadow = ContextCompat.getDrawable(getActivity(),R.drawable.tile_shadow); 

     RelativeLayout tileLayout = new RelativeLayout(getActivity()); 
     RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

     tileLayout.setBackground(drawableShadow); 
     tileLayout.setPadding(R.dimen.tilePadding,R.dimen.tilePadding,R.dimen.tilePadding,R.dimen.tilePadding); 
     tileLayout.setLayoutParams(rlp); 
     tileLayout.setId(999); 

     RelativeLayout.LayoutParams titleLayout = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

     TextView titleView = new TextView(getActivity()); 
     titleView.setText("Testing title with test"); 
     titleView.setId(111); 
     titleView.setTextColor(R.color.white); 
     titleView.setLayoutParams(titleLayout); 

     tileLayout.addView(titleView); 

     RelativeLayout.LayoutParams textLeftLayout = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
     textLeftLayout.addRule(RelativeLayout.BELOW, titleView.getId()); 

     TextView startHView = new TextView(getActivity()); 
     startHView.setText("left Text"); 
     startHView.setId(222); 
     startHView.setTextColor(R.color.white); 
     startHView.setLayoutParams(textLeftLayout); 

     tileLayout.addView(startHView); 

     RelativeLayout.LayoutParams textRightLayout = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
     textRightLayout.addRule(RelativeLayout.BELOW, titleView.getId()); 
     textRightLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     textLeftLayout.addRule(RelativeLayout.ALIGN_RIGHT, startHView.getId()); 

     TextView startMView = new TextView(getActivity()); 
     startMView.setText("Right Text"); 
     startMView.setId(333); 
     startMView.setTextColor(R.color.white); 
     startMView.setLayoutParams(textRightLayout); 

     tileLayout.addView(startMView); 

     RelativeLayout parentView = (RelativeLayout)mainView.findViewById(R.id.homeLayout); 
     parentView.addView(tileLayout); 
    } 

출력 : enter image description here

+0

@MikeM합니다. 배경색을 흰색으로보고 있으면 해결해야합니다 (검정색 또는 다른 색상으로 변경) – Zoe

+0

@MikeM Drawable 객체 (** tileLayout.setBackground (drawableShadow) **)로 밝은 녹색으로 배경색을 설정합니다. –

+1

음, 좋아, 글쎄,'tileLayout.setPadding()'호출이 잘못되었다. 'R.dimen'은 실제 값이 아닌 Resource ID입니다. 실제 값을 얻으려면'getResources(). getDimension (R.dimen.tilePadding)'을 사용하십시오. –

답변

0

대신 런타임 동안 상대 레이아웃을 만드는, 당신은 간단하게 만들 수 이미 존재하는 하나, 모든 자녀와 함께 표시 ... 예 :

if (myCondition) { 
    myLayout = (RelativeLayout) myContext.findViewById(R.id.myLayoutID); 
    myLayout.setVisibilty(View.visible); 
    myLayoutTextView = (TextView) myContext.findViewById(R.id.myTextView); 
    myLayoutTextView.setVisibility(View.VISIBLE); 
} 

이 경우 당신은 또한 당신이 레이아웃이 활성화되면 클릭 수있게되고 원하는 레이아웃에 버튼이, 당신은 할 수 있습니다 :

Button myButton = (Button) findViewById(R.id.myButton); 
myButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     if (myCondition) { 
      //Do Stuff Here 
     } 
}); 
관련 문제