我有两种不同的布局,title_bar 和 bottom_bar。bottom_bar 有 3 个不同的 ImageButton:usersButton、activtyButton 和 scanButton,title_bar 有一个 TextView 和几个 ImageView,我感兴趣的 ImageView 是 refreshButton。这是bottom_bar的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bottomToolBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#000000"
android:tileMode="repeat"
android:gravity="bottom" >
<ImageButton
android:id="@+id/usersButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_weight="2"
android:background="#000000"
android:src="@drawable/users" />
<ImageButton
android:id="@+id/activtyButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_weight="2"
android:background="#000000"
android:src="@drawable/activity" />
<ImageButton
android:id="@+id/scanButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_weight="2"
android:background="#000000"
android:src="@drawable/scan" />
</LinearLayout>
和title_bar xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/titleBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ff0000"
android:tileMode="repeat"
android:gravity="top" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="43dip"
android:src="@drawable/top_bar"
/>
<ImageView
android:id="@+id/refreshButton"
android:layout_width="55dip"
android:layout_height="38dip"
android:layout_gravity="right"
android:layout_marginTop="2dip"
android:src="@drawable/bar_refresh"/>
</FrameLayout>
我想做的是仅在按下 usersButton 和 activtyButton 时才显示 refreshButton,但不显示 scanButton,但现在它在所有 3 个按钮中都存在。我似乎很难看到bottom_bar和title_bar之间的通信位置是关于显示哪些视图,而使用cordova作为bottom和title之间的中间人则更加复杂。底栏的java代码是:
public final class BottomBar implements OnClickListener {
private ImageButton usersButton_;
private ImageButton activityButton_;
private ImageButton scanButton_;
private Activity activity_;
public BottomBar(LinearLayout layout, Activity activity) {
activity_ = activity;
LayoutInflater inflater;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout bottomBar = (LinearLayout) inflater.inflate(
R.layout.bottom_bar, null);
layout.addView(bottomBar);
usersButton_ = (ImageButton) layout.findViewById(R.id.usersButton);
activityButton_ = (ImageButton) layout.findViewById(R.id.activtyButton);
scanButton_ = (ImageButton) layout.findViewById(R.id.scanButton);
usersButton_.setOnClickListener(this);
activityButton_.setOnClickListener(this);
scanButton_.setOnClickListener(this);
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout titleBar = (FrameLayout) inflater.inflate(
R.layout.title_bar, null);
layout.addView(titleBar, 0);
ImageView backButton = (ImageView) layout.findViewById(R.id.backButton);
backButton.setVisibility(View.GONE);
}
public void onClick(View view) {
Log.d("getClass", activity_.getClass().getName());
if (activity_.getClass().getName().contains("CameraActivity")
|| activity_.getClass().getName()
.contains("RedemptionActivity"))
activity_.finish();
String app_name = ((App) activity_.getApplication())
.getAppName();
Log.d("app name", app_name);
if (view == usersButton_) {
Log.d("onClick", "usersButton");
Intent intent = new Intent("com.sampleapp.LOAD_URL");
intent.putExtra(
"url",
"file:///android_asset/www/index.html#activeTab=Tab%253ATerm%253A1&_=GenericScreen&limit=30&offset=0&page=TabbedPage%253ATerm%253A1&app_name="
+ app_name);
activity_.sendBroadcast(intent);
} else if (view == activityButton_) {
Log.d("onClick", "activityButton");
Intent intent = new Intent("com.samplepp.LOAD_URL.LOAD_URL");
intent.putExtra(
"url",
"file:///android_asset/www/index.html#activeTab=Tab%253ATerm%253A2&_=GenericScreen&limit=30&offset=0&page=TabbedPage%253ATerm%253A2&app_name="
+ app_name);
activity_.sendBroadcast(intent);
} else if (view == scanButton_) {
Log.d("onClick", "scanButton");
Intent intent = new Intent(activity_, CameraActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity_.startActivity(intent);
}
}
}
title_bar 的 java 代码:
public class Title extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
final CallbackContext callbackContext) throws JSONException {
Log.d("Title", action);
if (action.equals("change")) {
Intent intent = new Intent("com.sampleapp.CHANGE_TITLE");
intent.putExtra("title", args.getString(0));
this.cordova.getActivity().sendBroadcast(intent);
return true;
}
return false;
}
}
最后是cordova的java代码:
public class Cordova extends DroidGap {
TextView title;
ImageView backButton;
ImageView refreshButton;
private void changeTitle(String url) {
if (url.contains("Term%253A1"))
title.setText("App Users");
if (url.contains("Term%253A2"))
title.setText("User Activity");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty("loadUrlTimeoutValue", 60000);
registerReceiver(mHandleUrlReceiver, new IntentFilter(
"com.sampleapp.LOAD_URL"));
registerReceiver(mHandleTitleReceiver, new IntentFilter(
"com.sampleapp.CHANGE_TITLE"));
this.init();
this.appView.clearCache(true);
this.appView.clearHistory();
new BottomBar(this.root, this);
backButton = (ImageView) root.findViewById(R.id.backButton);
backButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
appView.goBack();
backButton.setVisibility(View.GONE);
changeTitle(appView.getUrl());
}
});
/*refreshButton = (ImageView) root.findViewById(R.id.refreshButton);
refreshButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
}
});*/
title = (TextView) this.root.findViewById(R.id.title);
spinnerStart("Loading", "Please wait...");
if (this.getIntent().hasExtra("json")) {
String json = this.getIntent().getExtras().getString("json");
Log.d("CORDOVA JSON", json);
try {
JSONObject jObject = new JSONObject(json);
Log.d("CORDOVA JSON", jObject.getString("app_name"));
((RobotFruitMerchant) this.getApplication())
.setSessionId(jObject.getString("session_id"));
String url = "file:///android_asset/www/index.html#activeTab=Tab%253ATerm%253A1&_=GenericScreen&page=TabbedPage%253ATerm%253A1&limit=30&offset=0&sessionId="
+ jObject.getString("session_id")
+ "&userId="
+ jObject.getJSONObject("user").getString("id")
+ "&app_name=" + jObject.getString("app_name");
super.loadUrl(url);
title.setText("App Users");
} catch (JSONException e) {
Log.e("JSONException", e.getMessage());
throw new RuntimeException(e);
}
} else {
String url = this.getIntent().getExtras().getString("url");
super.loadUrl(url);
}
}
private final BroadcastReceiver mHandleUrlReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String url = intent.getExtras().getString("url");
Log.d("CordovaReceiver", url);
appView.loadUrl(url);
changeTitle(url);
}
};
private final BroadcastReceiver mHandleTitleReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String title_text = intent.getExtras().getString("title");
title.setText(title_text);
backButton.setVisibility(View.VISIBLE);
}
};
}
抱歉,如果有点冗长,我比较新,希望尽可能具体。任何提示/建议/建议将不胜感激。