如何使用菜单选项格式化来自其他活动的活动

时间:2015-11-02 12:10:16

标签: android android-layout textview menuitem

您好我是Android新手,我想从菜单选项中更改活动的Textview大小和Textview颜色我不知道该怎么做

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

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

当我从当前布局菜单选项中单击显示选项时,需要调用新布局..在该布局中我创建了两个按钮一个加按钮,另一个是减号按钮,我希望这些按钮可以更改以前的布局。

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

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

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

为了帮助澄清,此问题与此App Crash When Trying to Change textsize in textView by clicking on button

的应用和作者相同

5 个答案:

答案 0 :(得分:6)

以DialogFragment的形式显示新布局。我们的想法是以DialogFragment弹出窗口的形式显示增加/减少选项。你可以从弹出窗口增加/减少大小。弹出窗口解除后,所选大小将设置为“活动”中的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>

答案 1 :(得分:2)

有时我会追踪用户活动的流程,以全面了解他们想要实现的目标,而且他们所做的设计选择通常基于他们在这里收到的答案,因此巢问题基于上一个问题中提供的解决方案。有时这很好,有时候这个人没有很好地掌握他们可用的设计方案。

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

您可以使用:

  1. 片段
  2. 共享偏好
  3. 意图
  4. 在菜单中设置调整大小选项
  5. 我选择展示这些片段,因为它对许多其他事物和有用的技能都有用。

    使用加号和减号按钮创建片段,而不是单独的活动。这种方式片段 可以直接访问所附活动的方法。

    使用菜单选项调用活动的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>
    

    创建片段:

    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>
    

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

答案 2 :(得分:1)

尝试

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id==R.id.display_option)
    {
        setContentView(R.layout.YOUR_LAYOUT)
    }


    return super.onOptionsItemSelected(item);
}

答案 3 :(得分:1)

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

答案 4 :(得分:0)

试试这个。希望它有效。

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

    }    
  }