0

기존 Linearlayout에 일부 LinearLayouts를 추가하고 싶습니다.비정상적으로 증가한 뷰를 LinearLayout에 프로그래밍 방식으로 추가합니다.

활동의 XML은 다음과 같이 보입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/popupLinearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorPopUp" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="none" > 

     <LinearLayout 
      android:id="@+id/ll_horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 
     </LinearLayout> 
    </HorizontalScrollView> 

</LinearLayout> 

활동의 코드 : 당신은 내가 버튼과 나는 팽창 후 텍스트 뷰를 추가 할 볼 수 있듯이

public class MainActivity extends Activity { 
    private Point p; 
    private PopupWindow popup; 
    private LinearLayout myLInearLayout; 
    private TextView valueTV; 
    private Button valueB; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button popUpButton = (Button) findViewById(R.id.open); 
     popUpButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       if (p != null) 
        showPopup(MainActivity.this, p); 
      } 
     }); 

    } 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     int[] location = new int[2]; 
     Button button = (Button) findViewById(R.id.open); 

     button.getLocationOnScreen(location); 
     // Initialize the Point with x, and y positions 
     p = new Point(); 
     p.x = location[0]; 
     p.y = location[1]; 

    } 

    // The method that displays the popup. 

    private void showPopup(final Activity context, Point p) { 

     Rect rectgle = new Rect(); 
     Window window = getWindow(); 
     window.getDecorView().getWindowVisibleDisplayFrame(rectgle); 

     int popupWidth = this.getResources().getDisplayMetrics().widthPixels; 
     int popupHeight = this.getResources().getDisplayMetrics().heightPixels/4; 

     // Inflate the popup_layout.xml 
     LinearLayout viewGroup = (LinearLayout) context 
       .findViewById(R.id.popupLinearLayout); 
     LayoutInflater layoutInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup); 

     // Creating the PopupWindow 
     popup = new PopupWindow(context); 
     popup.setContentView(layout); 
     popup.setWidth(popupWidth); 
     popup.setHeight(popupHeight); 
     popup.setOutsideTouchable(true); 
     popup.setFocusable(true); 
     popup.setAnimationStyle(R.style.PopupWindowAnimation); 

     // Some offset to align the popup a bit to the right, and a bit down, 
     // relative to button's position. 

     int OFFSET_X = 0; 
     int OFFSET_Y = 0; 

     // Clear the default translucent background 
     popup.setBackgroundDrawable(new BitmapDrawable()); 

     // Displaying the popup at the specified location, + offsets. 
     popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y 
       + OFFSET_Y); 

     // add LInearLayout 
     myLInearLayout = (LinearLayout) findViewById(R.id.ll_horizontal); 

     // add LayoutParams 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.MATCH_PARENT); 
     myLInearLayout.setOrientation(LinearLayout.HORIZONTAL); 

     // add textView 
     valueTV = new TextView(this); 
     valueTV.setText("The developer world is yours"); 
     valueTV.setId(5); 
     valueTV.setLayoutParams(params); 

     // add Button 
     valueB = new Button(this); 
     valueB.setText("thedeveloperworldisyours"); 
     valueB.setId(5); 

     // add the textView and the Button to LinearLayout 
     myLInearLayout.addView(valueTV); 
     myLInearLayout.addView(valueB); 
    } 

    @Override 
    public void onBackPressed() { 
     if (popup != null && popup.isShowing()) { 
      popup.dismiss(); 
      popup = null; 
     } else { 
      super.onBackPressed(); 
     } 
    } 

PopupWindow에 애니메이션을보고 보여줍니다. 일부보기를 ScrollView 안에 LinearLayout에 추가하고 싶습니다. 이것은 테스트 용이며, 나중에 전체 LinearLayouts를 ScrollView 내부의 LinearLayout에 추가하려고합니다. 애니메이션이 완벽하게 작동합니다. 일부 뷰는 프로그래밍 방식으로 추가 할 수 없습니다. 내가 얻는 모든 것은 뷰에서 라인을 추가하려고하는 NullPointerException입니다.

감사합니다.

답변

0

어떻게했는지 알아 냈습니다. 커스터마이즈하고 싶었던 LinearLayout에 대한 참조를 얻을 수 있도록 popup.xml을 부 풀려 야했습니다.

여기 사람이 같은 문제에 직면하는 경우 코드입니다 :

public class MainActivity extends Activity { 
    private Point p; 
    private PopupWindow popup; 
    private Button popUpButton; 
    private TextView valueTV; 
    private Button valueB; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     popUpButton = (Button) findViewById(R.id.open); 
     popUpButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       if (p != null) 
        showPopup(MainActivity.this, p); 
      } 
     }); 

    } 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     int[] location = new int[2]; 
     Button button = (Button) findViewById(R.id.open); 

     button.getLocationOnScreen(location); 
     // Initialize the Point with x, and y positions 
     p = new Point(); 
     p.x = location[0]; 
     p.y = location[1]; 

    } 

    // The method that displays the popup. 

    private void showPopup(final Activity context, Point p) { 

     Rect rectgle = new Rect(); 
     Window window = getWindow(); 
     window.getDecorView().getWindowVisibleDisplayFrame(rectgle); 

     int popupWidth = this.getResources().getDisplayMetrics().widthPixels; 
     int popupHeight = this.getResources().getDisplayMetrics().heightPixels/4; 

     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View contentView = inflater.inflate(R.layout.popup_layout, null); 
     ViewGroup dlgView = (ViewGroup) contentView 
       .findViewById(R.id.ll_horizontal); 

     // Creating the PopupWindow 
     popup = new PopupWindow(context); 
     popup.setContentView(contentView); 
     popup.setWidth(popupWidth); 
     popup.setHeight(popupHeight); 
     popup.setFocusable(true); 
     popup.setAnimationStyle(R.style.PopupWindowAnimation); 

     // Some offset to align the popup a bit to the right, and a bit down, 
     // relative to button's position. 

     int OFFSET_X = 0; 
     int OFFSET_Y = 0; 

     // Clear the default translucent background 
     popup.setBackgroundDrawable(new BitmapDrawable()); 

     // add textView 
     valueTV = new TextView(this); 
     valueTV.setText("The developer world is yours"); 
     valueTV.setId(5); 

     // add Button 
     valueB = new Button(this); 
     valueB.setText("thedeveloperworldisyours"); 
     valueB.setId(5); 

     // add the textView and the Button to LinearLayout 
     dlgView.addView(valueTV); 
     dlgView.addView(valueB); 

     // Displaying the popup at the specified location, + offsets. 
     popup.showAtLocation(popUpButton, Gravity.NO_GRAVITY, p.x + OFFSET_X, 
       p.y + OFFSET_Y); 
    } 
} 
0
Try this way,hope this will help you to solve your problem. 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button popUpButton = (Button) findViewById(R.id.open); 
     int[] location = new int[2]; 
     popUpButton.getLocationOnScreen(location); 
     // Initialize the Point with x, and y positions 
     p = new Point(); 
     p.x = location[0]; 
     p.y = location[1]; 
     popUpButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       if (p != null) 
        showPopup(p); 
      } 
     }); 

    } 

    // The method that displays the popup_layout. 

    private void showPopup(Point p) { 

     Rect rectgle = new Rect(); 
     Window window = getWindow(); 
     window.getDecorView().getWindowVisibleDisplayFrame(rectgle); 

     int popupWidth = this.getResources().getDisplayMetrics().widthPixels; 
     int popupHeight = this.getResources().getDisplayMetrics().heightPixels/4; 

     // Inflate the popup_layout.xml 

     LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popupLinearLayout); 
     View layout = LayoutInflater.from(this).inflate(R.layout.popup_layout, viewGroup); 

     // Creating the PopupWindow 
     popup = new PopupWindow(this); 
     popup.setContentView(layout); 
     popup.setWidth(popupWidth); 
     popup.setHeight(popupHeight); 
     popup.setOutsideTouchable(true); 
     popup.setFocusable(true); 

     // popup.setAnimationStyle(R.style.PopupWindowAnimation); 

     // Some offset to align the popup_layout a bit to the right, and a bit down, 
     // relative to button's position. 

     int OFFSET_X = 0; 
     int OFFSET_Y = 0; 

     // Clear the default translucent background 
     popup.setBackgroundDrawable(new BitmapDrawable()); 

     // Displaying the popup_layout at the specified location, + offsets. 
     popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y); 

     // add LInearLayout 
     myLInearLayout = (LinearLayout) layout.findViewById(R.id.ll_horizontal); 

     // add LayoutParams 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT); 
     myLInearLayout.setOrientation(LinearLayout.HORIZONTAL); 

     // add textView 
     valueTV = new TextView(this); 
     valueTV.setText("The developer world is yours"); 
     valueTV.setId(5); 
     valueTV.setLayoutParams(params); 

     // add Button 
     valueB = new Button(this); 
     valueB.setText("thedeveloperworldisyours"); 
     valueB.setId(5); 

     // add the textView and the Button to LinearLayout 
     myLInearLayout.addView(valueTV); 
     myLInearLayout.addView(valueB); 
    } 
+0

안녕, 귀하의 제안에 감사드립니다! 나는 방금 이것을 시도했다. 그러나 불행히도 그것은 나를 돕지 않습니다. :/ 지금까지 내가 보았 듯이 Point-Object의 초기화를 변경했습니다. 그러나 LinearLayout을 사용하려고하면 NullPointerException이 발생합니다. 이 줄에서는 예 : myLInearLayout.setOrientation (LinearLayout.HORIZONTAL); – Lyte

관련 문제