2013-07-31 4 views
0

이것은 내 코드이지만 null이 반환되는 이유를 모르십니까? 그러나 여기에 null 체크를 할 수는 있지만 잘못된 것이 있습니까? 이 null를 반환합니다 그래서 여기null을 반환하는 LayoutParams

tc.getLayoutParams() 

TextView descriptiontv = (TextView) findViewById(R.id.descriptiontv); 
    TextView tc = new TextView(c); 
    final PopupWindow windowPopUp = new PopupWindow(tc,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT ,false); 

    tc.setBackgroundResource(R.drawable.bg_training_baloon_hc_2x); 
    tc.setText("this is a demo test to check how it looks, i m just wanting to test whether it works or not"); 
    tc.setPadding(20, 20, 20, 20); 
    LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    params.setMargins(30, 0, 30, 0); 
    tc.setLayoutParams(params); 

답변

0

당신은 어떤 params 아직 제공하지 않았습니다. 어쩌면 당신은

descriptiontv.getLayoutParams() 

편집

params.setMargins(30, 0, 30, 0); 

params.leftMargin = 30; 
params.rightMargin = 30; 

를 변경 시도하고 변경을 의미

,785,

내 경험을 바탕으로

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
+0

을 죄송합니다, 나는 layoutparams를 설정하는 것을 잊어 버리십시오, 그들이 non-null 일 수있는 방법;) – Ankit

+0

나는 당신이 원하는 무엇을 확신하지 못합니다. xml에서 생성 된'TextView'에 대한'params'와 동일하게하고 싶습니까? 그렇다면 내 대답을 다시보십시오. – codeMagic

+0

textview에 약간의 여백을주고 싶지만,이 방법으로 작동하지 않는 것 같습니다. – Ankit

2

에, 당신은 당신이 그것의 레이아웃 매개 변수를 설정할 수 있습니다 전에과 같이, 먼저 부모보기로보기를 추가해야합니다 :

TextView calendarYear = new TextView(getActivity()); 
calendarYear.setText(calendar.getYear() + ""); 
calendarYear.setTextSize(20); 
calendarYear.setTypeface(null, Typeface.BOLD); 
calendarYear.setGravity(Gravity.CENTER); 
calendarYear.setTextColor(resources.getColor(android.R.color.black)); 
calendarItemsLinearLayout.addView(calendarYear); 

// We have to add the TextView to the layout first before we 
// can set the layout margins for it 
ViewGroup.LayoutParams layoutParams = calendarYear.getLayoutParams(); 
((LinearLayout.LayoutParams)layoutParams).setMargins(0, (int)(16 * density), 0, 0); 
calendarYear.setLayoutParams(layoutParams);