2013-05-24 5 views
0

LinearLayout 위치를 RelativeLayout으로 설정하려고 미쳤습니다. 당신은 아래RelativeLayout 안드로이드에서 LinearLayoutPosition을 설정하는 방법

내 클래스와 XML 파일을 찾을 수 있습니다 :

package com.example.layoutprova; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

public class MainActivity extends Activity implements OnClickListener{ 

private CustomView mCustomView; 
private Button mButton1; 
private RelativeLayout mRelativeLayout; 

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

    mRelativeLayout = (RelativeLayout) findViewById(R.id.mainlayout); 
    mButton1 = (Button) findViewById(R.id.button1); 
    mButton1.setOnClickListener(this); 
    mCustomView = new CustomView(this); 
    mRelativeLayout.addView(mCustomView); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    int buttonPressedId = v.getId(); 

    switch (buttonPressedId) { 
    case R.id.button1: { 
     mCustomView.setText("Change test"); 
     break; 
    } 
    default: { 
     break; 
    } 
    } 
} 

} 

자사의 XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/mainlayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="87dp" 
     android:layout_marginTop="131dp" 
     android:text="Button" /> 

</RelativeLayout> 
두 번째 XML 팽창

있는 CustomView 클래스 :

package com.example.layoutprova; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class CustomView extends LinearLayout { 
    public TextView mTitle1; 
    public Context mContext; 

    public CustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
     mContext = context; 
     init(); 
    } 

    public CustomView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     mContext = context; 
     init(); 
    } 

    private void init() { 
     View.inflate(mContext, R.layout.customlayout, this); 
     mTitle1 = (TextView) findViewById(R.id.title1); 
    } 
    public void setText(String text) { 
     mTitle1.setText(text); 
    } 
} 

자사의 XML을 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainlinear" 
    android:layout_width="200dp" 
    android:layout_height="60dp" 
    android:orientation="vertical" 
    android:background="@color/red" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/title1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:text="Title1" 
      android:textAlignment="center" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/data1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:text="Data1" 
      android:textAlignment="center" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/title2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:text="Title2" 
      android:textAlignment="center" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/data2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:text="Data2" 
      android:textAlignment="center" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

    </LinearLayout> 

</LinearLayout> 

MainActivity의 오른쪽 하단에 내 CustomView를 배치하고 싶습니다.

어떻게 할 수 있습니까?

감사

+0

당신은 완벽하게 작동 위대한의 LayoutParams –

답변

1
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); 
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
mRelativeLayout.addView(mCustomView,lp); 
+0

찾고 있습니다. 내가 thexml 파일에 param 옵션을 설정했기 때문에 나는 틀렸어.하지만 그들은 읽히지 않았다. – user1254938

관련 문제