如何使用菜单选项从另一个 activity 格式化一个 activity

How to format an activity from another activity using menu options

嗨,我是 Android 的新手,我想从菜单选项中更改 Activity 的文本视图大小和文本视图颜色,我不知道该怎么做

我创建了一个这样的菜单项

<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"  />

当我从当前布局菜单选项中单击 显示选项 时,需要调用一个新布局。我在该布局中创建了 两个按钮 一个是 Plus Button,另一个是 Minus Button,我希望这些按钮对之前的布局进行更改。

当我单击加号按钮时,文本视图文本想要更改,而当我单击减号按钮时,文本视图大小被减小。

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

<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>

详细信息Activity(这个Activity有一个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);
}

为了帮助澄清,这个问题与这个

是同一个应用和作者

尝试 this.Hopefully 它有效。

@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);

    }    
  }

尝试

@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;
}

以DialogFragment 的形式显示新布局。这个想法是以 DialogFragment 弹出窗口的形式显示 increase/Decrease 选项。您可以从弹出窗口中 increase/decrease 大小。弹出窗口关闭后,所选大小将设置为 Activity.

中的 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>

有时我会跟踪用户 activity 的流程以全面了解他们想要实现的目标,而且通常他们做出的设计选择是基于他们在此处收到的答案,因此nest问题是基于上一题提供的解决方案。有时这很好,有时这个人没有很好地掌握可供他们使用的设计选项。

所以回答问题:
有很多方法可以做到这一点(我的列表并不详尽),我建议了一些并演示了一个。

您可以使用:

  1. 碎片
  2. 共享偏好
  3. 意图
  4. 菜单中有调整大小选项

我选择演示片段,因为它对许多其他事情和有用的技能都有用。

使用加号和减号按钮创建一个片段,而不是单独的 activity。这样片段 可以直接访问附件 activity.

的方法

使用菜单选项调用 activity 的 showButtonOptions() 方法。

在你的 activity 内:

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>

创建片段:

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>

与大多数编程设计决策一样,可以有许多正确的解决方案,有些比其他的更好,有些只是首选设计选择的问题。在我们有选择之前,我们别无选择。