2017-10-26 4 views
0

내 목표는 frameLayout (자체 가중치 있음)에 toggleButton을 동적으로 추가하는 것이지만 button은 frameLayout의 너비의 절반이되어야합니다. xml에서 원하는 것을 만들 수 있지만 프로그래밍 방식으로이 작업을 수행하는 데 문제가 있습니다. buttonLayoutWrapper 그 내가 동적으로 추가 할 필요가 무엇으로 아이 ToggleButton이있어 :layout_weight 반으로 FrameLayout에 ToggleButton을 추가하는 데 프로그래밍 방식으로 추가하는 데 도움이 필요합니다.

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

    <LinearLayout 
     android:id="@+id/columns" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:baselineAligned="false" 
     android:orientation="horizontal" 
     android:weightSum="8"> 

     <FrameLayout 
      android:id="@+id/column_1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 


       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:background="@android:color/background_dark" /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/buttonLayoutWrapper" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:weightSum="2"> 

       <ToggleButton 
        android:layout_width="0dp" 
        android:layout_height="360dp" 
        android:layout_weight="1" 
        android:checked="true" 
        android:text="" 
        android:textOff="" 
        android:textOn=""/> 

      </LinearLayout> 

     </FrameLayout> 

    </LinearLayout> 

</LinearLayout> 

아이디로있는 LinearLayout에 특히주의를 기울이십시오 :

여기 내 XML입니다. 단순화를 위해 하나의 열만 보여주고 있지만, 7 회 더 반복합니다.

LinearLayout을 확장하고 ToggleButton을 추가하여 별도의 클래스를 만들려고했는데 그 방법으로 무게를 얻을 수 있다고 생각했지만 행운은 없었습니다.

내가 무엇을해도 ToggleButton이 layout_weight를 따르지 않는 것 같습니다.

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.column_1); 
    ToggleButton toggleButton = new ToggleButton(this); 
    LinearLayout.LayoutParams layoutWrapper = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 360, .5f); 
    toggleButton.setChecked(true); 
    toggleButton.setText(""); 
    toggleButton.setTextOn(""); 
    toggleButton.setTextOff(""); 
    toggleButton.setLayoutParams(layoutWrapper); 
    frameLayout.addView(toggleButton); 
} 

내가 XML 버전에서 작동하도록 레이아웃 무게에 대한 weightSum을 설정하는 부모의 LinearLayout이 필요 보인다 : 아래

프로그래밍이 일을하려고의 샘플입니다. 그러나, 나는 LinearLayout 부모를 추가하는 방법을 알아낼 수가 없어서, 컴파일 할 때 이상한 "LinearLayout을 FrameLayout으로 캐스팅 할 수 없음"오류없이 FrameLayout에 추가 할 수 있습니다.

도움이 될 것입니다.

답변

1

재 작성하는 것은

는 다음을보십시오. LinearLayoutToggleButton을 추가 한 다음 LinearLayoutFrameLayout에 추가합니다. 나는 그것이 당신이 찾고있는 것이라고 생각합니다. (이 일을 볼 수 있도록 XML에 LinearLayoutToggleButton을 주석 처리합니다.)

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     final float dpToPx = getResources().getDisplayMetrics().density; 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final LinearLayout linearLayout = new LinearLayout(this); 
     final FrameLayout.LayoutParams frameLP = 
      new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
             FrameLayout.LayoutParams.MATCH_PARENT); 
     linearLayout.setLayoutParams(frameLP); 
     linearLayout.setId(View.generateViewId()); // API 17+ needed 
     linearLayout.setWeightSum(2.0f); 

     final ToggleButton toggleButton = new ToggleButton(this); 
     final LinearLayout.LayoutParams linearLP = 
      new LinearLayout.LayoutParams(0, (int) (360 * dpToPx), 1.0f); 
     toggleButton.setLayoutParams(linearLP); 
     toggleButton.setChecked(true); 
     toggleButton.setText(""); 
     toggleButton.setTextOn(""); 
     toggleButton.setTextOff(""); 

     linearLayout.addView(toggleButton); 
     final FrameLayout frameLayout = (FrameLayout) findViewById(R.id.column_1); 
     frameLayout.addView(linearLayout); 
    } 
} 
+0

안녕 Cheticamp합니다. 응답 해 주셔서 감사합니다. 더 명확하게. 위 코드 예제는 오류없이 작동합니다. 위에 나열된 코드 주위에 선형 레이아웃을 래핑하려고 할 때만 캐스팅 오류가 발생합니다 (XML에서 수행중인 작업을 미러링하려고 시도). 또한 FrameLayout 내부에 토글 버튼이 있어야 배경 뷰 위에 살 수 있습니다. xml에서 수행중인 모든 코드 예제는 크게 감사하겠습니다. 감사! –

+0

@BrianBegun 귀하의 의견을 듣고 자세히 살펴보면 답변을 업데이트 할 수있었습니다. – Cheticamp

+0

그것이 내가 필요한 것입니다! 매력처럼 작동합니다! 감사! 나는 그것을 대답으로 표시 할 것이다. –

관련 문제