在Android中重用布局()
在我的项目中,我有一个适用于所有活动的通用布局。我正在尝试在Android中重用我的布局 主要活动在Android中重用布局(),android,android-layout,Android,Android Layout,在我的项目中,我有一个适用于所有活动的通用布局。我正在尝试在Android中重用我的布局 主要活动 public class MainActivity extends Activity implements OnClickListener { EditText emailEdit, passwordEdit; Button loginButton; TitleBarLayout titlebarLayout; String r; String rr;
public class MainActivity extends Activity implements OnClickListener {
EditText emailEdit, passwordEdit;
Button loginButton;
TitleBarLayout titlebarLayout;
String r;
String rr;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
emailEdit = (EditText) findViewById(R.id.etEmail);
passwordEdit = (EditText) findViewById(R.id.etPassword);
loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(this);
//titlebarLayout=(TitleBarLayout) findViewById(R.id.titlebar);
titlebarLayout=new TitleBarLayout(MainActivity.this);
titlebarLayout.setLeftButtonText("Refresh");
titlebarLayout.setRightButtonText("Logout");
titlebarLayout.setTitle("iProtect");
OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.left_button)
{}
else if(v.getId()==R.id.right_button)
{}
}
};
titlebarLayout.setLeftButtonOnClickListener(listener);
titlebarLayout.setRightButtonOnClickListener(listener);
}
}
login\u activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/layout_bg_color">
<include layout="@layout/titlebar_layout"
android:id="@+id/titlebar"/>
<EditText
android:id="@+id/etEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@layout/titlebar_layout"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dp"
android:layout_marginTop="100dip"
android:background="@drawable/emailedit"
android:inputType="textEmailAddress"
android:maxEms="20"
android:paddingLeft="70dip"
android:singleLine="true"
android:text="user@gmail.com" />
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etEmail"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dp"
android:layout_marginTop="0dip"
android:background="@drawable/passwordedit"
android:inputType="textPassword"
android:maxEms="20"
android:paddingLeft="70dip"
android:singleLine="true"
android:text="123456" />
<Button
android:id="@+id/loginButton"
android:layout_width="@dimen/login_button_width"
android:layout_height="@dimen/login_button_height"
android:layout_below="@+id/etPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
android:text="Login"
android:textStyle="bold"
android:background="@android:color/white"/>
<ImageView
android:id="@+id/ivFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_below="@+id/loginButton"
android:layout_marginTop="360dip"
android:adjustViewBounds="true"
android:baselineAlignBottom="false"
android:cropToPadding="false"
android:src="@drawable/footer" />
</RelativeLayout>
titleBarlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/titlebar_height"
android:background="@color/title_bg_color"
android:gravity="center_vertical"
android:orientation="horizontal">
<Button
android:id="@+id/left_button"
android:layout_width="@dimen/titlebar_button_width"
android:layout_height="@dimen/titlebar_button_height"
android:background="#000000"/>
<TextView
android:id="@+id/title_textview"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textStyle="bold" />
<Button
android:id="@+id/right_button"
android:layout_width="@dimen/titlebar_button_width"
android:layout_height="@dimen/titlebar_button_height" />
</LinearLayout>
和TitleBarLayout.java
public class TitleBarLayout extends LinearLayout {
private WeakReference<Activity> activityRef;
//Button leftButton, rightButton;
private View contentView;
Button leftButton,rightButton;
TextView titletext;
public TitleBarLayout(Activity a) {
super(a);
Log.i("TitleBar Layout", "Inside constructor");
activityRef = new WeakReference<Activity>(a);
inflateViewsFromXml();
setListenersOnViews();
setValuesOnViews();
}
private void setValuesOnViews() {
// TODO Auto-generated method stub
}
private void setListenersOnViews() {
// TODO Auto-generated method stub
}
private void inflateViewsFromXml() {
Activity a = activityRef.get();
LayoutInflater inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.titlebar_layout, null);
}
public void setLeftButtonText(int resID) {
}
public void setLeftButtonText(String resID) {
Log.v("Button", "Button Text="+resID);
if (contentView != null) {
leftButton = (Button)contentView.findViewById(R.id.left_button);
Log.i("ERROR", "Inside setLeftButtonText");
leftButton.setText(""+resID);
leftButton.setTextColor(Color.WHITE);
}
}
public void setLeftButtonOnClickListener(View.OnClickListener listener) {
leftButton.setOnClickListener(listener);
}
public void setRightButtonText(int resID) {
}
public void setRightButtonText(String resID) {
Log.v("Button", "Button Text="+resID);
if (contentView != null) {
Log.i("ERROR", "Inside setRightButtonText");
rightButton = (Button)contentView.findViewById(R.id.right_button);
rightButton.setTextColor(Color.BLACK);
rightButton.setText(""+resID);
}
}
public void setRightButtonOnClickListener(View.OnClickListener listener) {
rightButton.setOnClickListener(listener);
}
public void setTitle(int resID) {
}
public void setTitle(String resID) {
//@string/app_name
Log.v("TextView", "Text="+resID);
if (contentView == null) {
Log.i("ERROR", "Inside setTitle");
titletext = (TextView)contentView.findViewById(R.id.title_textview);
titletext.setText(""+resID);
}
}
}
我想在运行时在按钮上添加文本。但我无法获得它?我可以在Logcat上看到文本,但在按钮上看不到文本?您正在将布局扩展到变量视图“contentView”。 但是因为您的类是您在xml文件中膨胀的视图。您应按以下步骤充气:
inflater.inflate(R.layout.titlebar_layout, this);
然后布局将显示在屏幕上
通过这种方式,“充气”会将充气视图添加到类扩展的线性布局中。这是不正确的。安卓已经在膨胀标题栏,所以你不必再膨胀了。相反,您可以使用findViewById获取标题栏的XML信息。我想你可以使用@+id来完成这项工作,你提供给标签的内容
更新:为确保正确性而编辑,因为我误读了代码如果不使用Linearlayout进行扩展怎么办?如果覆盖onMeasure、onDraw和onLayout,则无法在xml文件中使用它。你试过我之前说的吗?titlebarLayout=新TitleBarLayoutMainActivity.this;如果这不正确,那么如何获取TitlebarLayout类的实例。此外,当我们充气时,ButtonfindViewByIdR.id.right_按钮中出现错误,而我们必须写入ButtoncontentView.findViewByIdR.id.left_按钮;我明白你的意思。然而,我只是编辑了答案以确保正确。