2014-10-17 110 views
0

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

对不起,很长的帖子..

片段:

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

那么,什么是问题:之后您可以(在其他按钮,例如在您的点击收听)

在活动的地方或OnClickListener动态改变按钮的知名度? – 2014-10-17 08:50:57

+0

该按钮不会显示在屏幕的右侧,我的理论是,我无法访问我的片段中的activity xml文件中的schemeContainer – 2014-10-17 09:09:36

回答

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?

+0

我试过这个,但是我可以运行代码,在那里我创建按钮和布局,但是当我尝试将其全部添加到viewGroup时,它全部崩溃 – 2014-10-17 09:15:17

+0

尝试在onActivityCreated方法中运行代码,而不是在onCreateView中。可能是它的帮助。 – 2014-10-17 09:19:58

0

添加您的按钮,在XML布局一般,但能见度设置了

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

现在创建时,你的按钮不考虑布局在开始。当消失时,按钮不可见,不占用任何空间,并且不能被点击/按下。

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

也许我不够具体,但需要动态添加按钮。 – 2014-10-17 09:27:54

相关问题