如何使用菜单选项从其他活动格式化活动

嗨,我是新来的Android,我想改变文本视图大小和Textview的菜单选项的颜色我不知道该怎么做

我创build了一个像这样的菜单项

<item android:id="@+id/menu_item_share" android:title="Share" android:showAsAction="ifRoom|withText" android:orderInCategory="100" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" /> <item android:id="@+id/display" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="Display Option" android:actionLayout="@layout/custom_setting" /> 

当我点击显示选项从当前布局菜单选项一个新的布局希望被称为..在该布局中,我已经创build了两个button之一是加号button ,另一个是减号button ,我希望这些button来更改以前的布局。

当我点击加号button文本视图文本想要更改,当我点击减号button文本视图大小减less。

菜单项被点击时的自定义布局代码

 <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.aeiltech.sidd.CustomSetting" android:background="@mipmap/bg"> <ImageButton android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/close" android:layout_above="@+id/textView" android:layout_toRightOf="@+id/minus" android:layout_toEndOf="@+id/minus" android:id="@+id/close" /> <ImageButton android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/plusign" android:layout_marginTop="110dp" android:id="@+id/plus" android:layout_alignParentTop="true" android:layout_alignLeft="@+id/textView" android:layout_alignStart="@+id/textView" /> <ImageButton android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/minusign" android:id="@+id/minus" android:layout_alignTop="@+id/plus" android:layout_alignRight="@+id/textView" android:layout_alignEnd="@+id/textView" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Change Text Size" android:id="@+id/textView" android:layout_above="@+id/minus" android:layout_centerHorizontal="true" android:textColor="#FFFFFF" /> </RelativeLayout> 

DetailActivity(这个活动有一个Textview)

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detail_activity); detailtext= (TextView) findViewById(R.id.detail); dbHelper = new SqlLiteDbHelper(this); try { dbHelper.openDataBase(); } catch (SQLException e) { e.printStackTrace(); } sqLiteDatabase = dbHelper.getReadableDatabase(); cursor=dbHelper.getdetails(sqLiteDatabase, selectedData); if(cursor.moveToFirst()) { detailtext.setText(cursor.getString(0)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_detail, menu); MenuItem shareItem = menu.findItem(R.id.menu_item_share); mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (id==R.id.display_option) { LayoutInflater layoutInflater= (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); layoutInflater.inflate(R.layout.custom_setting,null); } return super.onOptionsItemSelected(item); } 

为了澄清,这个问题是相同的应用程序和作者作为这个应用程序崩溃当试图改变textView的textsize通过点击button

Solutions Collecting From Web of "如何使用菜单选项从其他活动格式化活动"

以DialogFragment的forms显示新的布局。 这个想法是以DialogFragmentpopup窗口的forms显示增加/减less选项。 你可以增加/减lesspopup窗口的大小。 一旦popupclosures选定的大小设置为您的活动中的TextView。

MainActivity.java

 public class MainActivity extends AppCompatActivity { public static int updatedSize; TextView greetingText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); greetingText = (TextView)findViewById(R.id.hello_world_text); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { mycustomLayout(); return true; } return super.onOptionsItemSelected(item); } public void mycustomLayout(){ FragmentManager manager = getFragmentManager(); PopUp popUp = new PopUp(new FragmentDismissHandler()); popUp.show(manager,"POPUP"); } public void refreshText(){ if( updatedSize>0) greetingText.setTextSize(updatedSize); } private class FragmentDismissHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); refreshText(); } } 

}

activity_main.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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/hello_world_text" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

PopUp.java(自定义布局为DialogFragment)

 public class PopUp extends DialogFragment implements View.OnClickListener{ Button increase,decrease,set; TextView sizeOfText; static int size; Handler handler; public PopUp(Handler handler) { this.handler = handler; } public PopUp(){ } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment getDialog().setTitle("change text size"); View view = inflater.inflate(R.layout.my_custom_layout, container, false); increase = (Button)view.findViewById(R.id.inc); increase.setOnClickListener(this); decrease = (Button)view.findViewById(R.id.dec); decrease.setOnClickListener(this); sizeOfText = (TextView)view.findViewById(R.id.size_text); sizeOfText.setText("text"); set = (Button)view.findViewById(R.id.ok); set.setOnClickListener(this); size = (int) sizeOfText.getTextSize(); return view; } @Override public void onClick(View view) { int id= view.getId(); if(id==R.id.inc){ size = size +1; sizeOfText.setText("text"); sizeOfText.setTextSize(size); } if(id== R.id.dec){ size = size - 1; sizeOfText.setText("text"); sizeOfText.setTextSize(size); } if(id== R.id.ok){ dismiss(); } } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); MainActivity.updatedSize = size; handler.sendEmptyMessage(0); } 

}

my_custom_layout.xml

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> <Button android:id="@+id/inc" android:text="increase" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/dec" android:text="decrease" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/size_text" android:padding="20dp" android:textColor="@android:color/black" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/ok" android:text="set" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

有时,我会追踪用户活动的stream程,以便全面了解他们想要实现的目标,而且他们所做的deviseselect往往是基于他们在此获得的答案,所以嵌套问题是基于前面的问题。 有时候这是好的,有时候人们并没有很好地把握他们可以使用的devisescheme。

所以要回答这个问题:
有很多方法可以做到这一点(我的清单并不详尽),我build议一些,并展示一个。

你可以使用:

  1. 片段
  2. 共享偏好
  3. 意图
  4. 在菜单中有resize的选项

我已经select了演示这些片段,因为它对许多其他的东西和有用的技巧都是有用的。

使用加号和减号button创build一个片段,而不是单独的活动。 这样片段可以直接访问附加活动的方法。

使用您的菜单选项来调用您的活动的showButtonOptions()方法。

在您的活动中:

 Fragment fragment; FrameLayout frameLayout; @Override protected void onCreate(Bundle savedInstanceState) { ..../ View view = this.findViewById(android.R.id.My_Activity); frameLayout = (FrameLayout) findViewById(R.id.fragFrame); ..../ } public void ChangeMyLayoutMethod(args){ ..../ //use args to make change. } public void showButtonOptions() { // Create new fragment and transaction fragment = new MyPlusAndMinusButtonFragment(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.fragFrame, fragment); fragmentTransaction.commit(); frameLayout.setVisibility(View.VISIBLE); } public void returnToActivity() { frameLayout.setVisibility(View.GONE); // To do manage your back stack. } 

在你的Activity xml中

 <RelativeLayout ... or whatever ...> .../ Details of that activity <FrameLayout android:id="@+id/fragFrame" android:layout_below="@+id/wherever" android:layout_height="your choice" android:layout_width="your choice" android:layout_marginTop=".. etc" android:visibility="gone"> </RelativeLayout> 

创build一个片段:

 public class MyPlusAndMinusButtonFragment extends Fragment { FragmentManager fragmentManager; ..../ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.My_Plus_And_Minus_Button_Fragment, container, false); fragmentManager = getFragmentManager(); ...../ return view; } //To Do // Manage button clicks// onClick(){ // Call the parent's activity's // Cast this activity to type of parent activity. ((MyActivity) getActivity()).ChangeMyLayoutMethod(args); } 

片段布局,My_Plus_And_Minus_Button_Fragment.xml:

 <FrameLayout .....> <RelativeLayout ....> <ImageButtons etc... ..../ </RelativeLayout> </FrameLayout> 

与大多数编程devise决策一样,可以有许多正确的解决scheme,有些比其他的更好,有些仅仅是优先deviseselect的问题。 在我们有select之前,我们别无select。

尝试

 @Override public boolean onOptionsItemSelected(MenuItem item) { if (id==R.id.display_option) { setContentView(R.layout.YOUR_LAYOUT) } return super.onOptionsItemSelected(item); } 
  @Override public boolean onOptionsItemSelected(MenuItem item) { if (id==R.id.display_option) { myCustomLayout(); } return super.onOptionsItemSelected(item); } public void myCustomLayout(){ LinearLayout ll = new LinearLayout(getActivity()); ll.setOrientation(LinearLayout.VERTICAL); View customView = LayoutInflater.from(getActivity()).inflate(R.layout.my_custom_layout,null,false); ll.addView(customView); parentView.addView(ll); // parentView is a layout in your activity xml; } 

试试这个,很好用。

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuInflater inflater=getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } 

// OnOptionItemSelected

  @Override public boolean OnOptionItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.menu_item_share: Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show(); return true; default: return super.onOptionsItemSelected(item); } }