我要疯了,试图将我的 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>
膨胀第二个 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 的右下角。
我该怎么做?
谢谢