0

LinearLayout の位置を RelativeLayout に設定しようとして夢中になっています。

以下に、私のクラスと 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>

2 番目の 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>

CustomView を MainActivity の右下隅に配置したいと思います。

どうすればできますか?

ありがとう

4

1 に答える 1

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);
于 2013-05-24T21:19:18.877 に答える