从片段添加按钮到另一个视图

时间:2014-10-17 08:48:20

标签: android xml listview button fragment

我在左侧有一个片段活动,当我点击列表视图中的项目时,我想在右侧添加一个按钮。

很抱歉很长的帖子..

片段:

public class BoardFragment extends Fragment implements Button.OnClickListener{

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private Button bCreate;
private Button bViewMed;
private DatePicker datePicker;
private Button bSchemeSelection;
private TextView txt;
private ListView listView;
private OnFragmentInteractionListener mListener;
private ViewGroup viewGroup;

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = null;
    view = inflater.inflate(R.layout.fragment_board, container, false);
    bCreate = (Button) view.findViewById(R.id.bCreateScheme);
    bCreate.setOnClickListener(this);

    bViewMed = (Button) view.findViewById(R.id.bSeeMessage);
    bViewMed.setOnClickListener(this);

    bSchemeSelection = (Button) view.findViewById(R.id.bSelection);
    bSchemeSelection.setOnClickListener(this);

    datePicker = (DatePicker) view.findViewById(R.id.datePicker);
    txt = (TextView) view.findViewById(R.id.createrHeader);
    listView = (ListView) view.findViewById(R.id.listActivities);
    viewGroup = (ViewGroup) view.findViewById(R.id.schemeContainer);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            //String value = listView.getSelectedItem().toString();
            Toast.makeText(adapterView.getContext(),"hej",Toast.LENGTH_SHORT).show();

            LinearLayout ll = new LinearLayout(adapterView.getContext());
            LinearLayout.LayoutParams ll_settings =
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT);
            ll.setLayoutParams(ll_settings);

            ll.setOrientation(LinearLayout.HORIZONTAL);

            Button bb = new Button(adapterView.getContext());
            bb.setText("hej");
            RelativeLayout.LayoutParams bb_settings = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            bb.setLayoutParams(bb_settings);
            ll.addView(bb);

            viewGroup.addView(ll);
        }
    });

    return view;
}

活动

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class BoardCreator extends Activity
    implements BoardFragment.OnFragmentInteractionListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_board_creator);
    if(savedInstanceState ==null){
        getFragmentManager().beginTransaction()
                .add(R.id.toolbar,new BoardFragment())
                .commit();
    }
}

public void onFragmentInteraction(Uri uri){
    /*TextView txt = (TextView) findViewById(R.id.createrHeader);
    txt.setVisibility(View.VISIBLE);
    ListView listView = (ListView) findViewById(R.id.listActivities);
    listView.setVisibility(View.VISIBLE);*/
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.board_creator, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
    }
}

XML活动:

<LinearLayout 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"
android:orientation="horizontal"
tools:context="com.example.karljohan_schmidt.myapplication.BoardCreator">

<LinearLayout
    android:layout_width="240dp"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </FrameLayout>



</LinearLayout>

<FrameLayout
    android:id="@+id/schemeContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ScrollView>
</FrameLayout>


</LinearLayout>

XML片段:

<LinearLayout 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:orientation="vertical"
tools:context="com.example.karljohan_schmidt.myapplication.BoardFragment">

<!-- TODO: Update blank fragment layout -->

<Button
    android:id="@+id/bCreateScheme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Opret skema"/>

<Button
    android:id="@+id/bSeeMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Se meddelelser"/>

<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:calendarViewShown="false">
    </DatePicker>

<Button
    android:id="@+id/bSelection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Vis skema for valgte dag"
    />

<TextView
    android:id="@+id/createrHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Tryk på aktivitet for at tilføje:"
    android:visibility="invisible"/>
<ListView
    android:id="@+id/listActivities"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:entries="@array/activities"
    android:clickable="true"
    android:visibility="invisible"
    android:focusable="false">
</ListView>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

试试此代码

Button myButton=new Button(this);    
myButton.setId(btn);    
myButton.setBackgroundResource(R.drawable.image);    
myButton.setMinimumHeight(150);    
myButton.setMinimumWidth(150);      
Relativelayout.addView(myButton);     

其他属性是可选的。你也可以RelativeLayout选择你选择的任何布局。

请参阅此链接以获取更多信息:How to add a button dynamically in Android?

答案 1 :(得分:0)

通常在xml布局中添加按钮,但将可见性设置为

<Button
    android:id="@+id/myButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone">

现在,在开头创建布局时,不会考虑您的按钮。消失后,按钮不可见,不占用任何空间,无法点击/按下。之后,您可以动态更改按钮可见性(例如,在另一个按钮中的单击侦听器中)

活动中的某处或OnClickListener:

findViewById(R.id.myButton).setVisibility(View.VISIBLE);