我创建了一个名为editor.xml的xml文件,其中包含一个FrameLayout.在我的主要活动中,我正在尝试将自定义片段添加到FrameLayout中.
我在尝试添加片段时收到的错误是:
FragmentTransaction类型中的方法add(int,Fragment)不适用于参数(int,editorFrag)
然而,我的editorFrag扩展了Fragment,所以我很困惑为什么会发生这种情况.下面是我提到的文件的代码.任何帮助表示赞赏.
Editor.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
editorFrag.java
public class editorFrag extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.newlevel, container, false);
}
}
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
public class editorActivity extends FragmentActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
// Check that the activity is using the layout version with the fragment_container FrameLayout
if(findViewById(R.id.fragment_container) != null)
{
// if we …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-framelayout
我是Android开发的新手,我正在尝试处理从片段类到我的活动的多次按钮点击.通过在我的fragment类中创建一个监听器,然后让activity类实现该接口,我能够弄清楚如何处理一次单击.
myFragment.java
onResetGridListener mCallback;
// Container activity must implement this interface
public interface onResetGridListener
{
public void ResetGridClicked();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tilemap, container, false);
Button button = (Button) view.findViewById(R.id.resetGrid_button);
// A simple OnClickListener for our button. You can see here how a Fragment can encapsulate
// logic and views to build out re-usable Activity components.
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v) …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个具有一些任意值的简单可滚动JTable,并将其放在JPanel中.我遇到了一些奇怪的结果,其中JTable出现,但只显示列名称,似乎挤压剩余的行.表格也出现在面板的中央,我希望它位于顶部.
// Panel to hold our layer information
JPanel layerPanel = new JPanel(new GridBagLayout());
layerPanel.setBackground(Color.LIGHT_GRAY);
layerPanel.setPreferredSize(new Dimension(200, 200));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(0,5,0,5);
c.weightx = 1; c.weighty = 0.0;
c.fill = GridBagConstraints.BOTH;
c.gridx = 0; c.gridy = 0;
String[] columns = { "Show", "Layer" };
Object[][] data = {{"x", "Layer1"},
{"x", "Layer2"}};
// Construct our table to hold our list of layers
JTable layerTable = new JTable(data, columns);
layerTable.getColumnModel().getColumn(0).setPreferredWidth(64);
layerPanel.add(new JScrollPane(layerTable), c);
frame.add(layerPanel);
Run Code Online (Sandbox Code Playgroud)
任何帮助是值得赞赏的,当我使用GridLayout(1,0)而不是使用GridBagLayout时,代码可以工作,所以我觉得我使用错了.
感谢您的时间.